G
G
Genri_Rus2020-07-10 00:23:22
WordPress
Genri_Rus, 2020-07-10 00:23:22

Why is wp_query not working correctly when using pagination?

In general, I give an example of my wp_query request when filtering products:

$args = array(
  'post_type'	   => 'product',
  'posts_per_page' => 2,
  'meta_key'	   => '_price',
  'orderby'	   => 'meta_value_num',
  'order'		   => 'ASC',
  'paged'		   => $current_page,
  'tax_query'	   => array(
    'taxonomy' => 'product_cat',
    'field'    => 'id',
    'terms'    => $current_cat
  )
);

$query_pagination = new WP_Query( $args );

This code (when filtering, I did not use paged and posts_per_page I had -1 ) works just perfect, the $current_cat variable is the current category

So when paginating (I use ajax loading when scrolling (scroll)) as soon as I added 'paged' = > $current_page, wp_query request started behaving strangely - it completely ignores tax_query, i.e. displays all products as if they are all in the same category

How can I fix this ? Or did I do something wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
Genri_Rus, 2020-07-10
@Genri_Rus

In general, no one noticed the campaign, maybe this is not only my frequent mistake)
I forgot to specify the second array in this place:

'tax_query'	=> array(
  array => (
    'taxonomy' => 'product_cat',
    'field'    => 'id',
    'terms'    => $current_cat
  )
)

And here is an example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question