Wordpress pagination not working with search page

In my search page, My query returns 11 pages with this search: ?q=2s=chem

But when I try to access another page other than the first, like ?q=2s=chempaged=2 for example, WordPress shows error not found.

Note: If I leave empty s variable in URL, I have no problem.

This is my WP_Query :

$args = array(

'number' = $number,
'offset' = $paged ? ($paged - 1) * $number : 0,
's' = $_GET['s'],
'meta_query' = array(
    'relation' = 'OR',
    [
        'key'          = 'comp_name',
        'value'        = $_GET['s'],
        'compare'      = 'LIKE'
    ]
),);

Someone knows how to fix this problem?

Topic wp-query php location-search Wordpress search

Category Web


by default, wordpress has it's own main query in search page, in your case you didn't changed the main query, and just create a custom query

paged is a reserved query variable which is used first by your main query, when your main query dosent have same results, you face a 404 error you have 3 choices:

1. change paged variable

you can rename your page variable from paged to page or something else which is not used by wordpress by default

2. override 404 page

function override_404() 
{
    if ( is_search() && isset($_GET['s'])) {
        global $wp_query;
        $wp_query->is_404 = false;
    }
}
add_action('init', 'override_404');

3. override main query ( best )

function change_search_query() {
    if ( is_search() && isset($_GET['s'])) {
        $args = array();
        query_posts( $args );
    }
}
add_action('init', 'change_search_query');

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.