1

I need to query posts using certain taxonomy category, but more than one category of the taxonomy, as I can with posts categories..

For example, I have the Portfolio custom posts, and the Project Type to organize it (same as categories); So, we have Animation, draw, icons and painting types of project. I need to create an loop that will query animations and painting types only (query more than one king of portfolio type).

I thinked that tax_query would do it, but this tax_query selects only posts that are in the animations and paintings at the same time

My last code was:

$args['tax_query'] = array(
 array(
'taxonomy'  => 'project-type',
'field'  => 'id',
'terms'  => array(1,2) // don't work as I need, it's a exclusion)
)

Thank you in advance!

1 Answer 1

2

This should give you an OR relationship:

$args['tax_query'] = array(
  array(
    'taxonomy'  => 'category',
    'field'  => 'id',
    'terms'  => array(1,2),
    'operator' => 'IN'
  )
);

As would this:

$args['tax_query'] = array(
  'relation' => 'OR',
  array(
    'taxonomy'  => 'project-type',
    'field'  => 'id',
    'terms'  => array(1)
  ),
  array(
    'taxonomy'  => 'project-type',
    'field'  => 'id',
    'terms'  => array(2)
  )
);

Be aware that that will become increasingly inefficient as you add more terms. It should perform tolerably with just two. The first version is neater and probably performs better in general, unless you need to search by multiple taxonomies.

2
  • Doesn't exist a flexible function to do this? Thought I'd add more terms as the categories are increasing... Commented Jul 28, 2013 at 22:51
  • @LeonardoGirardi : See the edit.
    – s_ha_dum
    Commented Jul 28, 2013 at 22:52

Your Answer

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

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