public
Created

  • Download Gist
search.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
<?php
 
// Main search query
if( have_posts() ):
while( have_posts() ): the_post();
get_template_part( 'loop', 'search' );
endwhile;
 
// If no results, display random listing
else:
 
$args = array(
'posts_per_page' => 10,
'orderby' => 'rand',
);
$random = new WP_Query( $args );
if( $random->have_posts() ):
echo '<h2>No search results found, but here\'s some posts that might interest you</h2>';
 
while( $random->have_posts() ): $random->the_post();
get_template_part( 'loop', 'search' );
endwhile;
 
endif;
wp_reset_query();
endif;

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.