Yo guys, I am writing related posts method and I am trying to get all posts that have one or more terms in one or more taxonomies. Here is the sample of what my function spits out:
array(
'post_type' =>
array (
0 => string 'post',
1 => string 'event',
2 => string 'quiz',
3 => string 'newsletter'
),
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'OR',
array(
'taxonomy' => 'tax1',
'field' => 'term_taxonomy_id',
'terms' => array(...some ids...)
),
array(
'taxonomy' => 'tax2',
'field' => 'term_taxonomy_id',
'terms' => array(...some ids...)
)
)
)
The resulting query SQL gives me this:
wp_term_relationships.term_taxonomy_id IN
(58,47,98,75,90,100,101,29,30,102,99,60,52,64,91,44,27)
AND tt1.term_taxonomy_id IN (5,14,18,22) )
It gives me wrong results, namely the only left side of AND passes.
If I change the AND to OR
wp_term_relationships.term_taxonomy_id IN
(58,47,98,75,90,100,101,29,30,102,99,60,52,64,91,44,27)
OR tt1.term_taxonomy_id IN (5,14,18,22) )
It returns the desired results!
Does anyone know what am I missing? Any help would be appreciated! I even updated my Wordpress to the latest version!
UPDATE
I've came up with the not perfect solution to this:
Because I am using term_taxonomy_id
I can just add all the IDS and remove the taxonomy specification like this:
'tax_query' => array(
array(
'field' => 'term_taxonomy_id',
'terms' => array(...all ids...)
)
)
And it works! This is a quick solution, although it will work in all of the cases since I can select the desired taxonomies and exclude the non desirable in the process of generation of the query ARGS,
I would still love to know why it was ignoring my OR relation
in the first place? Is this a bug or I simply do not understand what OR relation
is meant to be?
Thanks peeps!