‘category__and’ for custom taxonomy?

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’.

Read More

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

$args = array ( ..?.. )

Related posts

1 comment

  1. 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.

Comments are closed.