WP_Query for table of contents - php

I am currently putting this array into my table of contents. To create something like this...
<?php
$args = array(
'post_category' => 'live',
'post_status' => 'publish',
‘order’ => ‘ASC’,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print the_field('venue');
the_excerpt();
endwhile;
wp_reset_postdata();
?>
When I add a new custom post the information is displaying on the same line.
I want each post to start on a new line.

Use tag div for record in 1 one.
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
$count = 1;
while ( $loop->have_posts() ) : $loop->the_post();
echo "<div class='item item-".$count."'>";
echo the_field( 'venue' );
the_excerpt();
echo "</div>";
$count++;
endwhile;
wp_reset_postdata();
?>

<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo the_field( 'venue' );
the_excerpt();
echo PHP_EOL;
endwhile;
wp_reset_postdata();
?>
In case of HTML
<?php
$args = [
'post_category' => 'live',
'post_status' => 'publish',
'order' => 'ASC',
];
$loop = new WP_Query( $args );
?>
<div class="list">
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="list-item">
<?php echo the_field( 'venue' ); ?>
<?php the_excerpt(); ?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>

Related

Product category in repeater fields

Im trying to display specific product categories in an ACF repeater field.
This keeps outputting all product categories.
How do I go to output single category per repeater field?
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_category_ids = get_sub_field('project_category');
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
'taxonomy' => 'product_cat',
'terms' => $product_category_ids
),
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' ); ?>
<?php
endwhile;
} else {
echo __( 'No products found' );
}
wp_reset_postdata();
?>
<?php endwhile; ?>
</ul>
This worked for me:
<?php if( have_rows('product_categories') ): ?>
<ul class="products">
<?php while( have_rows('product_categories') ): the_row(); ?>
<?php
$product_cats = get_sub_field('product_category');
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'posts_per_page' => '12',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $product_cats,
'operator' => 'IN',
'field' => 'slug'
),
)
);
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php endwhile; ?>
</ul>

Group by a custom value field wordpress posts

I have several posts with a custom field named "series". I want to group all posts by this custom field and below of this i want to list all posts which have not this custom field.
First i wanted to get grouped custom field value and then to be able to query again for all posts with this custom value key and value. But even trying to get the unique custom values does not work.
What i tried is this:
<?php
function query_group_by_filter($groupby){
global $wpdb;
return $wpdb->postmeta . '.meta_key = "series"';
}
?>
<?php add_filter('posts_groupby', 'query_group_by_filter'); ?>
<?php $states = new WP_Query(array(
'meta_key' => 'series',
'ignore_sticky_posts' => 1
));
?>
<?php remove_filter('posts_groupby', 'query_group_by_filter'); ?>
<ul>
<?php
while ( $states->have_posts() ) : $states->the_post();
$mykey_values = get_post_custom_values( 'series' );
foreach ( $mykey_values as $key => $value ) {
echo "<li>$key => $value ( 'series' )</li>";
}
endwhile;
?>
</ul>
Whats wrong with this code?
WP_Meta_Query
All posts with custom value:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'series',
'value' => 'my-val',
'compare' => '='
),
array(
'key' => 'series',
'value' => '',
'compare' => '!='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>
And without values:
<?php
$args = array(
'post_type' => 'my-post-type',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'series',
'value' => '',
'compare' => '='
)
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) : ?>
<ul>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li id="post-<?php the_ID(); ?>">
<?php the_title(); ?>
</li>
<?php
endwhile;
wp_reset_postdata(); ?>
</ul>
<?php endif; ?>

Show most recent post - if "yes" chosen as radio button value

Here is the setup for my radio button:
I can't seem to get the posts that are valued at yes to display in the loop..
Here is my loop:
<?php
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);"></div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
This is what worked for me:
<?php
$args = array(
'post_type' => 'event',
'showposts' => 1,
'orderby' => 'date',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'value' => 1,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"><div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div></a>
<?php endwhile; else: ?>
<?php endif; ?>
if ( have_posts() ) should reference the query: if ( $the_query->have_posts() ).
Also, as has already been mentioned, you should use 'posts_per_page' => 1, instead of numberposts.
Try using a meta_query to accomplish this. Update your $args with something like the following:
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => 'LIKE',
'value' => 'yes',
),
),
);
You also need to update if ( have_posts() ) to if ( $the_query->have_posts() ).
More information about meta_query can be found here: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Instead of using both post_per_page and number posts you can use any one of your choice
Try any of the Methods as available below. As per the ACF the Method 1 is suggested but as for as the WordPress you can retrieve information based on Method 2 also.
Method:1
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
//Your Code over here to Manipulate
<?php endwhile; else: ?>
<?php endif; ?>
Method:2
Try the meta query with compare as = operator in the meta_query.
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => '=',
'value' => 'yes',
),
),
);
I tested the code on my machine and it works fine! Just (as already mentioned from others) the if() statement is wrong, change it to:
if ( $the_query->have_posts() )
But now I have a really silly question... After creating the ACF field, have you saved some posts (events) with the meta field (yes or no) ? If not, WP wont find anything because the postmeta is not saved into the DB.
Did you took a look into the DB if the postmeta is saved correctly?

How to get recent product image in woocoomerce

i wanted to add recent woocommerce product in home page.
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 1, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product;
?>
Content Here
<?php endwhile;
wp_reset_query(); ?>
i wanted to recent product featured image in woocommerce which code i write to get image.
Try below code:
<ul>
<?php
$args = array( 'post_type' => 'product', 'stock' => 1, 'posts_per_page' => 4, 'orderby' =>'date','order' => 'DESC' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
<li class="span3">
<a id="id-<?php the_id(); ?>" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="Placeholder" width="65px" height="115px" />';
?>
</a>
<?php woocommerce_template_loop_add_to_cart( $loop->post, $product ); ?>
</li><!-- /span3 -->
<?php endwhile; ?>
<?php wp_reset_query(); ?>
This might help you :
$args = array(
'post_type' => 'product',
'posts_per_page' => 10,
'orderby' => 'date',
'fields' => 'ids',
'order' => 'DESC');
$newly_arr = query_posts($args);
$all_images = array();
foreach ($newly_arr as $a){
$post_thumbnail_id = get_post_thumbnail_id($a);
$all_images[] = wp_get_attachment_url($post_thumbnail_id);
}
echo '<pre>';
print_r($all_images);
die;

Nest a while loop in an if else statement in WordPress

I have a while loop that gets all pages of a certain category on index.php which is my posts page.
However, I want to change the code below, so if it is the homepage, it does not run this loop but displays some text.
This code the loop works:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
?>
In this code, nothing loads on any page except the homepage:
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC' ,
'paged'=>$paged
);
if ( is_home() ) {
echo 'Welcome!';
} else if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
You need to wrap your query and loop in your is_home() condition
if ( !is_home() ) { // This is not the home page
// Add your query and loop here
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
while ( $loop->have_posts() ) : $loop->the_post();
?> <?php the_title();?>
endwhile;
} else { // This is the homepage
echo 'Some text here';
}
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$loop = new WP_Query( array(
'post_type' => 'story',
'cat' => $cat_id,
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC'
) );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $the_query->the_post();
if ( is_home() ) {
echo 'Welcome!';
}
else{?>
<h2><a href="<?php echo get_permalink(); ?>"><?php the_title();
}
<?php endwhile; endif; ?>

Categories