0

I'm trying to build a custom search form and I'd like to override the site's default posts per page limit but the hidden field I'm using doesn't seem to want to work at all:

I have:

<input type="hidden" value="-1" name="posts_per_page" />

..but this seems to be ignored when I'm inspecting $wp_query on the search results page (the search I'm doing matches 29 posts, but a count on $wp_query->posts comes in at 10. I've done multiple tests using different values to no avail.

The full custom search form looks like this:

<form role="search" method="get" id="search" action="/" class="col-xs-12">
    <input type="text" value="" name="s" id="s" />
    <input type="hidden" value="1" name="sentence" />
    <input type="hidden" value="-1" name="posts_per_page" />
    <input type="hidden" value="any" name="post_type" />
    <input type="submit" id="searchsubmit" value="Search" />
</form>

Is there something pretty basic I'm overlooking?

I had a bunch of pre_get_posts actions set up for tweaking stuff in the admin screens, but I've disabled all of these to see if they were interfering but again, no joy.

1 Answer 1

0

You can set the posts_per_page with pre_get_posts action like this:

add_action( 'pre_get_posts', 'my_set_posts_per_page' );

function my_set_posts_per_page( $query ){
  if ( is_admin() ){
    return;
  }

  //Check if this is a search
  if ( isset( $query->query['s'] ) ) {
    $query->set( 'posts_per_page', -1 );
  }
}
1
  • Sorry for the late response, this is exactly what I ended up doing :)
    – efreeman
    Sep 1, 2017 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.