display posts tag__in or latest if there are not enough result
-
Hello, I want to display 4 posts in ma page with tag__in parameter.
But if my query return less than 4 posts, because I only have 2 posts tagged, I wan’t to add my two latest post to always display 4 posts.
How can I do that please, whithout making multiple Wp_query ?
This is my working bad code :$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) ); $args = array( 'posts_per_page' => 4, 'cat' => 5, 'post__not_in'=> array($post->ID), 'tag__in'=> $tag_ids ); $query = new WP_Query($args); $nbposts = $query->post_count; $ids = array($post->ID); while ( $query->have_posts() ) : $query->the_post(); $ids[] = get_the_ID(); ... my post content endwhile; wp_reset_postdata(); if($nbposts < 4){ $nbpost = 4 - $nbposts; $args['posts_per_page'] = $nbpost; $args['post__not_in'] = $ids; $args['tag__in'] = ''; $query = new WP_Query($args); while ( $query->have_posts() ) : $query->the_post(); ... my post content endwhile; wp_reset_postdata(); }
- The topic ‘display posts tag__in or latest if there are not enough result’ is closed to new replies.