0

this is my query:

  $p_query = new WP_Query(array(
    'post_type' => $post_type
    ,'post_status' => 'publish'
    ,'posts_per_page' => $per_page // 6
    ,'paged' => $page // 2
    ,'post__not_in' => array($first_post) // an ID
    ,'tax_query' => array(
       array(
         'taxonomy' => $data['tax'] // tax slug
         ,'field'    => 'slug'
         ,'terms' => $data['term'] // term slug
       )
    )
  ));

Pagination works properly without the tax_query. As soon as I add it, on page 2 something strange happens:

  • it returns the correct number of items (the remaining number of items)

  • the items are wrong: they are the initial ones again.

In other words, if the items per_page are 6, and the total are 8, on page 2 I'll get 2 items, but instead of being items 7,8, they'll be 1,2.

Why? What's wrong?

  • Nothing here looks wrong to me. Is $first_post still being set correctly? Are you inspecting $p_query to see this happen or is this based on your template code? – MikeNGarrett Jun 13 at 15:50
  • all the params are correct, I checked all. That's my own code. – Luca Reghellin Jun 13 at 15:51
  • Everything looks correct to me. Check to make sure you're not resetting the query early or having parameters overwritten. – MikeNGarrett Jun 13 at 16:01
0

I've found the solution by myself. In brief: use 'order' => 'DESC'


The long story:

Since I was doing the first page query via php (standard loop), and instead, the above is done via ajax, passing pagination (thus page 1 is php, the following are ajax), there was a mismatch about the order. I was still retrieving what is seemed to be page 1 posts because the query was done with the default ASC order, while the standard loop works with DESC order by default (date-based). So the posts retrived by ajax were in fact of page 2!

By inverting the order, I now get the right posts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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