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.

Is there a way to count all published posts from a custom taxonomy?

While looking around I found this snippet but I didn’t manage to get it to work …

global $wpdb;
$query = "
        SELECT COUNT( DISTINCT cat_posts.ID ) AS post_count
        FROM wp_term_taxonomy AS cat_term_taxonomy INNER JOIN wp_terms AS cat_terms ON
        cat_term_taxonomy.term_id = cat_terms.term_id
        INNER JOIN wp_term_relationships AS cat_term_relationships 
        ON cat_term_taxonomy.term_taxonomy_id = cat_term_relationships.term_taxonomy_id
        INNER JOIN wp_posts AS cat_posts 
        ON cat_term_relationships.object_id = cat_posts.ID
        WHERE cat_posts.post_status = 'publish' 
        AND cat_posts.post_type = 'post' 
        AND cat_term_taxonomy.taxonomy = 'YOUR-CUSTOM-TAXONOMY' 
        AND cat_terms.slug IN ('TERM-SLUG-1, TERM-SLUG-2')
    ";
return $wpdb->get_var($query);
share|improve this question

4 Answers 4

up vote 3 down vote accepted

Use an instance of WP_Query to query the database. http://codex.wordpress.org/Class_Reference/WP_Query

To query database for custom taxonomies use,

$query = new WP_Query( array( 'people' => 'bob' ) );

For more details on available options see: Taxonomy Parameters http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

Retrieve published posts using

'post_status' => 'publish'

Use found_posts to retrive the number of posts

$count = $query->found_posts;
share|improve this answer
function wp_get_productcat_postcount($id) {

    //return $count;
    $args = array(
      'post_type'     => 'product', //post type, I used 'product'
      'post_status'   => 'publish', // just tried to find all published post
      'posts_per_page' => -1,  //show all
      'tax_query' => array(
        'relation' => 'AND',
        array(
          'taxonomy' => 'product_cat',  //taxonomy name  here, I used 'product_cat'
          'field' => 'id',
          'terms' => array( $id )
        )
      )
    );

    $query = new WP_Query( $args);

    /*
    echo '<pre>';

    print_r($query->post_count);
    echo '</pre>';
    */

    return (int)$query->post_count;

}
share|improve this answer

You can do this with the current object with get_queried_object()

$posts = get_queried_object();
echo $posts->count;

Otherwise you may unnecessarily run a second query, right?

share|improve this answer

Wordpress adds attribute count to each taxonomy and updates it whenever a new post is inserted or updated. Because it does not want to go to database again and do calculations to get posts count in taxonomy.

echo $custom_tax_obj->count;
share|improve this answer

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.