Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created May 17, 2012 15:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save billerickson/2719520 to your computer and use it in GitHub Desktop.
Save billerickson/2719520 to your computer and use it in GitHub Desktop.
<?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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment