Wordpress - Count posts in custom taxonomy
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;
}
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;
Below code will get count of article from particular taxonomy
<?php
$terms = get_the_terms( $post->ID , 'your-taxonomy' );
foreach ( $terms as $term ) {
echo $term->count;
} ?>