Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've a custom post type 'post-type-x' and three custom taxonomies attached with it 'taxonomy-1', 'taxonomy-2' and 'taxonomy-3'.

I want to retrieve all the posts that have custom taxonomies 'taxonomy-1' and 'taxonomy-2'.

What will be the arguments array that I'll be passing in query_posts function?

$args = array ( ..?.. )
share|improve this question
1  
Hopefully you won't use query_posts at all: wordpress.stackexchange.com/questions/1753/… –  vancoder Aug 16 '13 at 22:17
    
vancoder: I appreciate this. But I don't know how it is linked to my question. My problem is that I can't design the arguments array that I need to send into query_posts or WP_Query object. Thanks. –  Omar Tariq Aug 16 '13 at 22:39

1 Answer 1

up vote 1 down vote accepted

I think this question can cause a misunderstanding. You say:

I want to retrieve all the posts that have custom taxonomies 'taxonomy-1' and 'taxonomy-2'

but

do you want post that have both taxonomy-1 and taxonomy-2 and taxonomy-3 is irrilevant?

$args = array(
  'post_type' => 'post-type-x',
  'tax_query' => array(
    'relation' => 'AND',
    array(
      'taxonomy' => 'taxonomy-1',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-1', array('fields' => 'ids', 'hide_empty' => false) )
    ),
    array(
      'taxonomy' => 'taxonomy-2',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-2', array('fields' => 'ids', 'hide_empty' => false) )
    )
  )
);

or

Do you want post that have both taxonomy-1 and taxonomy-2 but not taxonomy-3?

$args = array(
  'post_type' => 'post-type-x',
  'tax_query' => array(
    'relation' => 'AND',
    array(
      'taxonomy' => 'taxonomy-1',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-1', array('fields' => 'ids', 'hide_empty' => false) )
    ),
    array(
      'taxonomy' => 'taxonomy-2',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-2', array('fields' => 'ids', 'hide_empty' => false) )
    ),
    array(
      'taxonomy' => 'taxonomy-3',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-3', array('fields' => 'ids', 'hide_empty' => false) ),
      'operator' => 'NOT IN'
    )
  )
);

or

Do you want post that have taxonomy-1 or taxonomy-2 and taxonomy-3 is irrilevant?

$args = array(
  'post_type' => 'post-type-x',
  'tax_query' => array(
    'relation' => 'OR',
    array(
      'taxonomy' => 'taxonomy-1',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-1', array('fields' => 'ids', 'hide_empty' => false) )
    ),
    array(
      'taxonomy' => 'taxonomy-2',
      'field' => 'id',
      'terms' => get_terms( 'taxonomy-2', array('fields' => 'ids', 'hide_empty' => false) )
    )
  )
);

Note that these type of queries will be poor performant, because get_terms run a db query, so using code above you will have several queries that slow down the page view.

If in the file that contains this code you can access to some variables that contain an array of id (or slug) of the taxonomy terms you can use them and improve performance.

Alternative is create a custom db query using $wpdb->get_results with an appropriate SQL query.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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