If a WordPress query with taxonomy has less than X fill with another query

I’m a little stuck on a custom query.

Basically i have a query that gets related “companies” (maximum of 3) of the post. If the query doesn’t have 3 then use another query to fill the spaces (up to 3). I cant seem to get it to do what its suppose to do. Currently its showing 6??? Not quite sure where I’m going wrong!

Read More
$taxonomy = 'company';

// Get the post company
$terms = wp_get_post_terms( $the_post_id, $taxonomy );

$term_name = $terms[0]->name;

// Query for the related posts based on the company
$taxonomy_query = new WP_Query( array(

'tax_query' => array(
    array(
        'taxonomy' => $taxonomy,
        'field' => 'name',
        'terms'    => $term_name,
    ),
),
'post_type'=> 'post',
'post__not_in' => array($the_post_id),
'posts_per_page' => 3
) );

// Query for the non-related posts (use to fill the empty spaces)
$query = new WP_Query( array(

'category_name' => $cat_name,
'post_type'=> 'post',
'post__not_in' => array($the_post_id),
'posts_per_page' => 3
) );

I then have the output of the said querys

<div class="l-row">

    <?php $count = 0; ?>

    <?php if ( $taxonomy_query->have_posts() ) : ?>

        <?php while ( $taxonomy_query->have_posts() ) : $taxonomy_query->the_post(); ?>

        <?php $related_posts = get_post(); ?>

        <?php $count++; ?>

        <div class="l-col-sm-4">

            <?php _module( 'tile', array(
                 'post'    => $related_posts,
                 'image'   => true,
                 'excerpt' => false
             ) ); ?>
        </div>

        <?php endwhile; ?>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

    // New query to fill if the above query doesn't add up to 3

    <?php if ( $query->have_posts() ) : ?>

        <?php while ( $query->have_posts() ) : $query->the_post(); ?>

        <?php $related_posts = get_post(); ?>

        <?php $count++; ?>

        <div class="l-col-sm-4">

            <?php _module( 'tile', array(
                'post'    => $related_posts,
                'image'   => true,
                'excerpt' => false
            ) ); ?>

         </div>

        <?php if($count == 3) {
            break;
        } ?>

        <?php endwhile; ?>

    <?php endif; ?>

    <?php wp_reset_query(); ?>

</div>

Thanks in advance if anyone can point me in the right direction!

Related posts

1 comment

  1. your issue is here (assuming your code works)

    <?php if($count == 3) {
            break;
        } ?>
    

    If you have 3 posts in the first query, your count is 3, you increase the count on the 4th before the above statement and its now equal to 4 and continues without meeting your conditional.

    A better approach is to scrap the break and use the while statement

      <?php while ( $query->have_posts() && $count < 3 ) : $query->the_post(); ?>
    

Comments are closed.