Custom Search Page Pagination Not Working

You’re running into a conflict between your replacement query and the default search query that’s running behind the scenes.

Instead of running a new WP_Query, try modifying the default query by hooking to pre_get_posts:

function wpse276396_search_filter($query) {
  if ( !is_admin() && $query->is_main_query() ) {
    if ($query->is_search) {
      $text = urldecode( get_query_var('search_text') );
      $my_products = aws_search_byozan($text);
      $query->set('post_type', 'product');
      $query->set('posts_per_page', 2);
      $query->set('post__in', $my_products);
    }
  }
}

add_action('pre_get_posts','wpse276396_search_filter');

Then you’d output the default Loop in your template instead of your custom WP_Query Loop.

This has the advantage of not hitting the database once for the default query then disregarding the results and using your custom query to render the page instead, plus the paged and posts_per_page arguments are consistent with what WP expects.

Edit: I somehow managed to miss that this is in a page template. That’s enough WPSE for me for today, but I’ll update my answer to reflect that rather than modifying the default search query (which is still a better option unless you need your custom search to coexist with the built-in search form).

In this case what’s happening is WP is expecting the paged and posts_per_page arguments to apply to the main query which fetched your search Page, which means the instructions on the Codex page you linked absolutely do apply.

$my_products = aws_search_byozan($text);
global $query_string;

$query_args = explode("&", $query_string);
$search_query = array();

if( strlen($query_string) > 0 ) {
    foreach($query_args as $key => $string) {
        $query_split = explode("=", $string);
        $search_query[$query_split[0]] = urldecode($query_split[1]);
    } // foreach
} //if

global $paged;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;

$args = array(
    'post_type' => 'product',
    'posts_per_page' => 2,
    'paged' => $paged,
    'post__in' => $my_products

);
$loop = new WP_Query( array_merge($search_query, $args ) );

Leave a Comment