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 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!

share|improve this question
    
let me know if it works without these parameters,I need similar functions to implement –  Md Toufiqul Islam Oct 21 '12 at 9:25
    
nope, it's not working either... :( –  Stefano Oct 21 '12 at 10:45

1 Answer 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);
share|improve this answer
    
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). –  Stefano Oct 21 '12 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'? –  Md Toufiqul Islam Oct 21 '12 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) –  Stefano Oct 21 '12 at 13:00
    
You simply forgot the compare arg. –  kaiser Oct 22 '12 at 17:22

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.