Count number of posts which exist in 2 taxonomies?

Here is how I ended up solving it, with some thanks to Samuel for pointing at tax_query, although that was only part of the picture. I needed to create a loop, and I did it like so:

$ones = get_terms( array(
    'taxonomy' => 'ONE',
    'hide_empty' => false,
    'fields' => 'id=>slug' 
) );

$fruits = get_terms( array(
    'taxonomy' => 'FRUIT',
    'hide_empty' => false,
    'fields' => 'id=>slug' 
) );


foreach ($fruits as $fruit) {

foreach ($ones as $one) {

$args = array(
    'post_type' => 'my-post-type',
 'post_status'=>'publish',
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'ONE',
            'field'    => 'slug',
            'terms'    => array( $one ),
        ),
        array(
            'taxonomy' => 'FRUIT',
            'field'    => 'slug',
            'terms'    => array( $fruit ),
        ),
    ),
);
$query = new WP_Query( $args );

echo $fruit .": " . $one . ": " . $query->post_count . "<br>";

}
}

Leave a Comment