I have a pretty standard loop which outputs “Sorry, No Posts Found” when a search result comes up empty.

if ( have_posts() ) : while ( have_posts() ) : the_post();
    get_template_part ( 'templates/post', 'main' );
endwhile; else:
    echo 'Sorry, No Posts Found';
endif; 

How can I show alternate posts below that message?

So if someone searches for “Tacos” and no posts are found, the results page would say:

Sorry, No Posts Found. But here’s some posts about Pizza…

1 Answer
1

I’ve never seen it done before but the logic of the PHP IF/ELSE should mean you could simply plug a new query in after the ELSE:

if ( have_posts() ) : while ( have_posts() ) : the_post();
    get_template_part ( 'templates/post', 'main' );
    endwhile; 
else:
    $the_query = new WP_Query( $args );
    if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : 
           $the_query->the_post();
        endwhile;
    endif;

endif;

This is untested on my end.

Leave a Reply

Your email address will not be published. Required fields are marked *