Support » Fixing WordPress » using posts_per_page in search form not working

  • Greeting all,

    A client wants to limit the number or items returned for a search result page to 5 items.

    I’ve looked through the classes.php code and see the use of ‘posts_per_page’. So I’ve added a hidden form field of this name to my search form. Set the value to 5. When I search I still get the default of 10. I’ve tried to trace through the code and cannot determin what I’m doing wrong.

    My WP is 2.0.3. Here is my form code:
    <form method=”get” id=”searchform” action=”<?php bloginfo(‘home’); ?>/”>

    <input type=”text” class=”textbox” name=”s” id=”s” value=”<?php echo wp_specialchars($s, 1); ?>” />
    <input type=”hidden” class=”textbox” name=”posts_per_page” id=”posts_per_page” value=”5″ />
    <input type=”image” alt=”go” class=”imgbutton” name=”imageField” src=”<?php bloginfo(‘template_url’); ?>/images/gosearchbutton.jpg” />

    </form>

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi,

    I had a very similar problem, in that I wanted to have my index and archive pages show only one post per page, but I wanted all of my search results to show up on one page. After a lot of failure and searching, I found the solution in somebody else’s problem:

    Instead of changing the search form, your client can change the search results page. Just before the Loop, add the following code:

    <?php
    $wp_query->query_vars[“posts_per_page”] = 5;
    $wp_query->get_posts();
    ?>

    My own problem was that I was using <?php query_posts(“posts_per_page=5”); ?>. As the user on the other topic explains, using query_posts() restarts the query, whereas the other method above merely adds the post_per_page variable to the existing query (which is a search query in the case of the search results page).

    I hope this helps!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘using posts_per_page in search form not working’ is closed to new replies.