0

I am new to WordPress and try to set pagination based on a custom query, by using Query found the result from the meta key custom field and product result.

But the issue comes here in pagination query result is based on $wp_query.

How can I set the value of custom query into the $wp_query

    //$s='GPO2'
    //query for meta search 
    // searching  the result from the meta field 
     $q1 = array(
        'fields' => 'ids',
        'posts_per_page' => -1,
        'post_type' => 'products',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'product_sku',
                'value' => sanitize_text_field( $s ),
                'compare' => 'LIKE'
            ),
            array(
                'key' => '  
    product_color_0_color',
                'value' => sanitize_text_field($s),
                'compare' => 'LIKE'
            )
        )
    );
            $loop = new WP_Query( $q1 );

    // search the result from main table         

     $q2 = &new WP_Query("s=$s&showposts=-1&post_type=products&fields=ids"); 

    //here by using these queries  found the unique ids

    if(!empty($loop->posts) && !empty($q2->posts))
    {
      $unique = array_unique( array_merge( $loop->posts, $q2->posts ) );  
    }
    elseif(!empty($loop->posts) && empty($q2->posts))
    {

      $unique=$loop->posts;
    }else{
      $unique=$q2->posts;
    }



    if ( get_query_var('paged') ) {
        $paged = get_query_var('paged');
    } elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
        $paged = get_query_var('page');
    } else {
        $paged = 1;
    }


    $posts= array(
        'post_type' => 'products',
        'post__in' => $unique,
          'paged' => $paged,
        'post_status' => 'publish',
        'posts_per_page' => 10
    );

    $allsearch= &new WP_Query( $posts );
    the_posts_pagination( array(
            'format'             => 'page/%#%/?s='.$msgSearch.'',
            'prev_text' => '<span class="screen-reader-text">' . __( 'Previous page', 'twentyseventeen' ) . '</span>',
                    'next_text' => '<span class="screen-reader-text">' . __( 'Next page', 'twentyseventeen' ) . '</span>',
                    'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyseventeen' ) . ' </span>',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $allsearch->max_num_pages,
            'rewrite' => false
                ) );

1 Answer 1

0

The the_posts_pagination probably relies on the main query. You will need to "hack" the query global var or simple replace the main query (if you're not using it afterwards).

So replacing the main query:

//$allsearch= &new WP_Query( $posts );
query_posts($posts);

Or replacing the main query vary

$allsearch= &new WP_Query( $posts );
$backup = $GLOBALS['wp_query'];
$GLOBALS['wp_query'] = $allsearch;

the_posts_pagination();

$GLOBALS['wp_query'] = $backup;

Also, I've noted you used a variable named $posts. I've already faced some issues using global variable names with queries, so I do not recommend using a variable named $posts

4
  • thanks for your reply , i have already done this part and it update the $GLOBALS['wp_query'] and pagination number showing in correct way but some pagination link showing page not found .I think it still using main query Dec 6, 2017 at 6:57
  • Strange. The next paginated page is calling the same query? Also, try re-save the permalink settings just to make sure it's not a missing rule. Dec 6, 2017 at 13:47
  • ya it was also strange for me , At the last i have solved the error :) Dec 7, 2017 at 8:07
  • Glad you found a solution. Dec 7, 2017 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.