2

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...

6

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.

  • This should work. I had the same idea on this :-) – Pieter Goosen Dec 9 '14 at 15:46
  • This answer is correct although the difficulty lies in getting from taco to pizza and any other probable null result search terms to similar terms. – KnightHawk Dec 11 '14 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.