1

I want to count the number of posts that has simoultaneously 3 different taxonomies. THe code i'm using is this:

$products = get_posts(array(
    'post_type' => 'products',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'collection',
            'field' => 'slug',
            'terms' => array($current_collection),
            'operator' => 'IN'
        ),
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => array($current_type),
            'operator' => 'IN'
        ),
        array(
            'taxonomy' => 'color',
            'field' => 'slug',
            'terms' => array($current_type),
            'operator' => 'IN'
        )
    )   
));
$countpost = count($products);

The problem occours when the colors and type taxonomy term I'm looking for do not exists...

  • if the collection does not exists, the count is 0

  • if the type does not exists, it returns posts with the specified collection and color

  • it the color does not exists, it returns posts with the specified collection and type

what I was expenting, since the relation is AND, was that if one of the terms does not exists, the count should be always 0, instead of basically ignoring that term and considering only tho other two...

am I doing something wrong? how can I fix it?

thanks a lot!

2
  • let me know if it works without these parameters,I need similar functions to implement Oct 21, 2012 at 9:25
  • nope, it's not working either... :(
    – stefano1
    Oct 21, 2012 at 10:45

1 Answer 1

1

Try this again removing operator and relation arguments as follows: here is a useful link http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/

$products = get_posts(array(
    'post_type' => 'products',
    'posts_per_page' => -1,
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'collection',
            'field' => 'slug',
            'terms' => array($current_collection)
        ),
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => array($current_type)
        ),
        array(
            'taxonomy' => 'color',
            'field' => 'slug',
            'terms' => array($current_type)
        )
    )   
));
$countpost = count($products);
4
  • nope, not working either. even if I remove operators and relations the result is always the same: if the value of the color or the type does not exist in the database, it simply ignore that part and return all the posts, instad of 0 (no posts).
    – stefano1
    Oct 21, 2012 at 10:44
  • will you please explain about all three taxonomies used here? It worked for me where my taxonomies were post category and post tags. what are 'collection', 'type' and 'color'? Oct 21, 2012 at 10:58
  • collections are group of products, imagine it like a car brand, type is like the model of the car, and color obviously is a color of the single product (that can come in different colors)
    – stefano1
    Oct 21, 2012 at 13:00
  • You simply forgot the compare arg.
    – kaiser
    Oct 22, 2012 at 17:22

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.