WordPress function to count the number of posts

Get the number of posts from a category

<?php $args = array(
'cat' => 4 );
$the_query = new WP_Query( $args );
echo $the_query->found_posts; ?>

Get the number of posts from a post type

<?php $args = array(
'post_type' => 'videos');
$the_query = new WP_Query( $args );
echo $the_query->found_posts; ?>

Get the number of posts from a tag

<?php
$taxonomy = "post_tag"; 
$term_id = 2;
$term = get_term_by('id', $term_id, $taxonomy);
echo $term->count; ?>

Leave a Reply