This guest post was written by Leland of Theme Lab, where he has released over 50 WordPress themes. In addition to themes, Theme Lab also provides some WordPress guides. If you have WordPress knowledge and are interested in writing a post for Hack WordPress, please contact us.

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.

Digg This! | Stumble it! | Add to Del.icio.us | | Print This! |

There Are 3 Responses So Far. »

  1. 1 Kyle Eslick
    Wednesday, April 23rd, 2008 at 4:44 am

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



Leave A Comment