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.

What is the best method to count the number of posts in a post type that have a particular term? I don't believe get_posts accepts a term query and I have had no luck with new WP_Query, though I might be doing something wrong.

Usage example:

$posts = get_posts( array(
    'post_type' => 'inventory',
    array(
        'taxonomy' => 'status',
        'terms' => 'in-stock',
        'field' => 'slug'
    ),
) );
$count =  count( $posts );
echo $count;
share|improve this question
add comment

1 Answer 1

up vote 4 down vote accepted
$items = get_posts( array(
    'post_type'   => 'inventory',
    'numberposts' => -1,
    'taxonomy'    => 'status',
    'term'        => 'in-stock'
    ) );

$count =  count( $items );
echo $count;
share|improve this answer
    
that returns an array when echo'd and print_r dumps all the post data, without a count. Was definitely missing the tax_query though, thank you! –  mattrepublic Jun 23 '11 at 2:45
    
You just need to add that part back in like you had in the original. I edited answer to reflect this. –  mfields Jun 23 '11 at 2:54
    
I forgot to mention that I had left $count = count( $posts ); echo $count; in as well. Changing the status taxonomy term (tried three different terms) does not change the post count. So I'm assuming, unless you have an option to the contrary, that tax_query isn't passing the term. :/ –  mattrepublic Jun 23 '11 at 3:08
    
just updated again. There should be no need to use the taxonomy query if you only need to access one term of one taxonomy. –  mfields Jun 23 '11 at 3:23
add comment

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.