I have a search page with pagination, that is displaying the correct number of pages, however it the links seem to be forwarding to the 404 page.
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$search_args = array(
'posts_per_page' => 2,
's' => get_search_query(),
'post_status' => 'publish',
'post_type' => 'post',
'paged' => $paged,
);
$search = new WP_Query($search_args);
$big = 999999999; // need an unlikely integer
$pagination_args = array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'mid_size' => 2,
'total' => $search->max_num_pages
);
echo paginate_links($pagination_args);
Any help as to why these links might be broken would be a huge help!
have_posts()
andthe_post()
(without$search->
or anything like that) will have the correct posts already in it. Then you can use functions likepaginate_links()
without needing to specify any of the arguments.have_posts()
andthe_post()
mean that you wouldn't be able to give loop a search query from an input form?get_search_form()
or the widget, then you will be taken to/?s={search term here}
. On that page the main query will be populated with results for that search. The template that's loaded will be based on the hierarchy (so search.php if it exists, otherwise index.php).