Sometimes you may not want your search results to be limited by the confines of the standard WordPress Loop. This is a quick code hack to allow a search to return unlimited results, altering the standard WordPress Loop by using a custom query. You can do this in a few different ways. If you have a search template, in search.php you can simple add the following line of code above your Loop.

Find:

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Add:

<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

Make sure you put this code in your search.php only, unless you want unlimited posts on your index or archive pages. The -1 you see can be changed to any positive integer to limit the posts to a number you choose as well.

If you don’t have a search.php in your theme, the next level down in the Template Hierarchy is your Main Index Template, or index.php. You can use a conditional tag for the same effect.

For this we’ll use the same code as above, except wrap it in the is_search() conditional tag, like so:

<?php if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

You can use this technique to change the standard Loop limitations of archives, categories, tag pages, and even your main index template – but it would probably be easier to simply change your reading settings for that.

Leland Fiegel runs the popular WordPress website ThemeLab.com where he has released over 50 WordPress themes and also creates custom WordPress themes over at CustomTheme.com.

  1. Kyle Eslick says:

    Great post Leland! I think there are plenty of situations where displaying additional search results can be extremely useful.

  2. Patrick says:

    Thanks for the tip! 

    I’ll be making this update with the wpSearch plugin — brand new, but looks like it’s going to make my search results a whole lot better too.  You should check it out.

  3. Jojo says:

    Thank you! Works fine…

  4. Mau says:

    Great! That’s exactly what I needed!
    Thanks so much!!!

    Byez

  5. John says:

    Thanks, works great. Now if only I can get images to show in the search results. Any help would be greatly appreciated.

  6. bbbbbooooo says:

    you rock dude.

  7. Thanks, I’ve been looking for a good way to limit the number of search results. I know that wasn’t your intent with this post, but it helped me out. :D

  8. cornelius says:

    thanks a lot fot this!!

  9. Gustavo says:

    Thanks a lot. Works like a charm.

  10. prazetyo says:

    I tried to show the three posts on search results. but the result is still an error. On the search results already appeared three posts, but still look 10 post css.

    error image:
    http://www.flickr.com/photos/13655032@N02/4798344944/

    thank you

Trackbacks/Pingbacks »

  1. infomisa.net» Blog Archive » 10 Useful WordPress Search Code Snippets says:
  2. Resultados de búsqueda ilimitados | Todo bit says:
  3. 10 Useful WordPress Search Code Snippets « UKWDS! says: