0

I have a problem with category__in when I do a WP_Query.

$query_args = array(
    'post_type'  => 'posts',
    'nopaging'   => true,
);

$categories = array(1,8); // If I use this, I get 3 posts
$categories = array(8, 1); // If I use this, I get 2 posts
$query_args['category__in'] = $categories;

$query = new WP_Query($query_args);

Apparently I get different results when I pass it "array(1,8)" than when I pass it "array(8,1)".

Is the order important??? Or is this a bug??

UPDATE

This is the request for the case array(1,8)

string(571) "SELECT   wp_posts.* FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)  LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)  LEFT JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id) WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (1) 
  AND 
  tt1.term_taxonomy_id IN (1) 
  AND 
  tt2.term_taxonomy_id IN (1,8)
) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC "

and for array(8,1)

string(571) "SELECT   wp_posts.* FROM wp_posts  LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)  LEFT JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)  LEFT JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id) WHERE 1=1  AND ( 
  wp_term_relationships.term_taxonomy_id IN (8) 
  AND 
  tt1.term_taxonomy_id IN (8) 
  AND 
  tt2.term_taxonomy_id IN (1,8)
) AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC "
  • 1
    What's echo $query->request; for those two cases? – birgire Apr 25 '17 at 10:06
  • Updated. Should point that in neither case the amount of posts returned is correct. Could be because I have defined also an additional taxonomy on my site? – Enrique Moreno Tent Apr 25 '17 at 10:13
2

I solved it the following way

$query_args['tax_query'] = array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'id',
            'terms'    => $categories,
        ),
    );

Apparently I had to explicitely say that I was filtering through the taxonomy "category". I could see that thanks to the comment from @birgire. Thanks man!

Update

As petition, this is the request:

SELECT wp_posts.*
FROM wp_posts LEFT JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1 AND (wp_term_relationships.term_taxonomy_id IN (1,8)) 
    AND wp_posts.post_type = 'post'
    AND (wp_posts.post_status = 'publish'
        OR wp_posts.post_status = 'private')
GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC
  • It would be interesting to see the generated sql here for comparison. – birgire Apr 25 '17 at 10:42
  • 1
    I have pasted it. :) – Enrique Moreno Tent Apr 25 '17 at 11:34

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.