I'm looking to count how many post live within the custom post type called "videos", but only the ones from the category called "work".

<?php $count_posts = wp_count_posts('videos'); echo $count_posts->publish; // ?>

How can I adjust the above code to accomplish this?


7 Answers 7


An alternative solution using WP_Query would be:

$args = array(
  'cat' => 4,
  'post_type' => 'videos'
$the_query = new WP_Query( $args );
echo $the_query->found_posts;
  • Custom post types use taxonomies, not "categories" so this is wrong. Commented Feb 3, 2022 at 10:54

For a specific custom taxonomy try:

$the_query = new WP_Query( array(
    'post_type' => 'CUSTOM_POST_TYPE',
    'tax_query' => array(
            'taxonomy' => 'CUSTOM_TAXONOMY',
            'field' => 'id',
            'terms' => TERM_ID
) );
$count = $the_query->found_posts;

Documentation at https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  • Be sure to change to echo $count = $the_query->found_posts; if you want to show the actual result on the frontend. Commented Feb 3, 2022 at 10:40
  • 1
    This should be the accepted (or most upvoted) answer. Commented Feb 7, 2022 at 10:38

Found a solution.

<?php $posts = get_posts('post_type=videos&category=4'); 
$count = count($posts); 
echo $count; 
  • 11
    Really bad idea. What if you have 15000 posts? You put them ALL in memory? These are the kind of misconceptions that could wreck a production site.
    – Cranio
    Commented Mar 4, 2016 at 15:12
  • As @Cranio mentions above: terrible idea to get all the posts just to count them.
    – dhuyvetter
    Commented Jul 28, 2017 at 12:08
  • Keep in mind he posted that in 2013 before WP_Query was popular. Commented Feb 3, 2022 at 10:53

This also should work:

$category = get_term('work', 'category');
$posts_in_category = $category->count;
  • 6
    This will fail if the a taxonomy is assined to more than one post type and you just need the post count of one post type Commented Nov 10, 2015 at 9:15

I know that this is an old thread, but it shows up first in Google, so here is the REAL solution on how to do this.

$term = get_term( $termId, $taxonomy );
$total_in_term = $term->count;

So you need to pass the ID of the term and the taxonomy. This is the lightest weight solution as well as having the benefit of working with custom taxonomies.


Found this while looking into a similar thing myself so here's my solution in case it's useful for anyone else... Note: Harmonic's answer works, depending on scenario though it may be easier to do this instead:

$count = get_category($category->term_id)->category_count;

Where $category is your taxonomy object.

Important note here being that this assumes no other post_type uses the same taxonomy. Details: get_category() is actually a wrapper function of get_term().

In this case, get_term() has a name__like parameter that get_category() doesn't. There are probably other little differences too.

See: get_term() get_category


Basically if you do it with your found solution, you will waste quite much DB resources when you have lots of posts to fetch.

$query = new WP_Query();
echo $query->found_posts();

However WP_Query->found_posts just fetch 'posts_per_page' and do COUNT(*) mysql job for you. So I recommend you to use the latter one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.