How To: Make your WordPress Search Results Unlimited

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.

  • Leave a Comment
  • How To: Separate WordPress Comments and Trackbacks

    Note: For WordPress 2.7 and newer WordPress installations, you’ll instead want to use this tutorial.

    With all the WordPress themes available to WordPress users, it always surprises me how these incredible theme authors don’t take a few extra seconds to separate their theme’s trackbacks from the comments. It doesn’t look very professional and it can make it extremely difficult to follow a conversation in the comments.

    Separating your trackbacks and comments requires a minimal amount of coding work to set up. First, you’ll want to make a backup of your comments.php file just in case something goes wrong. Next, follow these three steps:

    1 ) Access your comments.php file and locate the following code:

    <?php foreach ($comments as $comment) : ?>

    Immediately after the above code, you’ll want to place this code:

    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type == 'comment') { ?>

    2 ) Next, you’ll want to scroll down a little bit and locate the following code:

    <?php endforeach; /* end for each comment */ ?>

    Immediately before the above code, you’ll want to place this code:

    <?php } /* End of is_comment statement */ ?>

    This will filter out all of the trackbacks and pingbacks from your main comments loop. Now we need to create a second comments loop to display the trackbacks and pingbacks.

    3 ) Almost immediately below the code from step 2 you should find this code:

    <?php else : // this is displayed if there are no comments so far ?>

    Immediately before the above code, you’ll want to place this code:

    <h3>Trackbacks</h3>
    <ol>
    <?php foreach ($comments as $comment) : ?>
    <?php $comment_type = get_comment_type(); ?>
    <?php if($comment_type != 'comment') { ?>
    <li><?php comment_author_link() ?></li>
    <?php } ?>
    <?php endforeach; ?>
    </ol>

    You can adjust this code to display how you want to, including using a different header if you have a specific look for your header 3.

  • Leave a Comment