I have a simple WP post query, which is supposed to load 8 posts per page, with a post navigation displayed below, like so:
$query_args = [
"post_type" => "post",
"post_status" => "publish",
"posts_per_page" => 8,
"paged" => ( get_query_var( "paged" ) ? get_query_var( "paged" ) : 1 )
];
// Get all the posts
$posts = new \WP_Query( $query_args );
$output = "";
if ( $posts->have_posts() ) {
while ( $posts->have_posts() ) {
$posts->the_post();
$output .= get_the_excerpt();
}
$output .= "<div id=\"post-navigation-links\">".
paginate_links( array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $posts->max_num_pages
) ).
"</div>";
} else {
$output = '<div id="no-posts-found">Nothing found</div>';
} // loop which is entered if no posts matching the query args have been found
wp_reset_postdata();
This all works when I load the page.
On that same page, I have a search field. If I submit that search field, I call exactly the same code as the one on page load via AJAX (code above) to replace the page content with the new result, just with the difference of the additionall "s" parameter in the $query_args
. Result of this: I get exactly the matching posts, with a proper pagination displayed below. When I however click on the pagination links on the updated page, I get a 400 Bad Request
and a 0
answer from the server (page all white and only containing a 0
). On the initial page load, pagination works perfectly.
When comparing the URLs after clicking on a page load:
Before content being replaced with string query (on page load), the pagination URLs have this structure and work perfectly:
https://example.com/page/3
After content being replaced with string query, the pagination URLs have this structure and result in 400 errors:
https://example.com/wp-admin/admin-ajax.php?ranum=0.825265473159516/page/3
So the problem seems to occur at the ajax handler. Any idea???
action
parameter.