404 page on custom search pagination

I have to kinds of search forms in my sidebar. Both of this forms are handled in search.php page by an if/else statement and write a custom query for both of them. I also replace the default wordpress query with mine so the the_posts_pagination works correctly. for my first search query, it’s working perfect and showing the right numbers, but in my second query, it shows the right page numbers but when i click on the numbers, 404 page!
Here is a piece of my code :

$paged = ( get_query_var( ‘paged’ ) ) ? get_query_var( ‘paged’ ) : 1;
if(fist form is filled) {

$adSearchQuery = new WP_Query(array(
‘post_type’ => ‘post’,
‘category__in’ => array(1,3,4),
‘posts_per_page’ => 3,
‘paged’ => $paged
));

$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $adSearchQuery;

// Show posts

the_posts_pagination();

$wp_query = NULL;
$wp_query = $temp_query;

wp_reset_postdata();
}
else {
$tax_query = array(
array(
‘taxonomy’ => ‘health’,
‘field’ => ‘term_id’,
‘terms’ => “126”
)
);
$aposts = new WP_Query(array(
‘type’ => ‘post’,
‘category_name’ => “test”,
‘tax_query’ => $tax_query,
‘posts_per_page’ => 3,
‘paged’ => $paged
));

$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $aposts;

// Show posts

the_posts_pagination();

$wp_query = NULL;
$wp_query = $temp_query;

wp_reset_postdata()
}

But I don’t know why the second pagination is not working though it has the right number of total posts and page numbers.

Read more here:: 404 page on custom search pagination

Leave a Reply

Your email address will not be published. Required fields are marked *