Tutorials WordPress

Display Total Number of WordPress Posts for Custom Post Type

Updated

Written by

Dave Warfel

Reading Time

1 minute

If you buy something from one of our links, we may earn a commission.

Do you want to display the total number of WordPress posts for a specific custom post type you’ve created? Use the following code to get the total number of published posts in a custom post type, and then display it in your theme.

Replace “post-type-name” with the name of the custom post type you created when using the register_post_type() function. This must match exactly.

<?php // Get total number of posts in post-type-name
	$count_posts = wp_count_posts('post-type-name');
	$total_posts = $count_posts->publish;
	echo $total_posts . ' custom posts. ';
?>

You can change the variable names so they are relevant to your custom post type.

Example

If your custom post type is called “Books”, your code would look like this:

<?php // Get total number of posts in "Books" post type
	$count_books = wp_count_posts('books');
	$total_books = $count_books->publish;
	echo $total_books . ' books. ';
?>

The variable in the echo statement will just return the number. You can change the word “books” to say whatever you want.

Get creative with how you use this, and share it in the comments.

Reference wp_count_posts() in the WordPress Codex.

Dave Warfel

LinkedIn  •  X (Twitter)
Dave has been working with WordPress since 2011. He's built 100s of client sites and almost a dozen of his own. He's tested almost every plugin you can think of, hosted with at least 10 different companies, and gone down every SEO rabbit hole you can imagine. When's he's not tinkering with new software, you'll find him in the mountains of Colorado, trail running, summiting peaks, and rippin' downhills on his mountain bike. 🏔️🏃🚴🤸

7 responses to “Display Total Number of WordPress Posts for Custom Post Type”

  1. Aman Avatar
    Aman

    Hi, how can I dynamically change ‘post-type-name’ of this line with custom fields ?
    $count_posts = wp_count_posts(‘post-type-name’);

    TIA :)

    1. Dave Warfel Avatar

      You can’t use the wp_count_posts() function to count posts that contain a certain custom field value. That function only works for checking the number of posted articles in a specific post type.

      You’d have to use a custom WP_Query, and then the count() PHP function, to pull the number of posts with a specific custom field value.

  2. Adam Avatar
    Adam


    $args = array(
    'post_type' => 'careers'
    );
    $the_query = new WP_Query($args);
    $totalpost = $the_query->found_posts;
    echo $totalpost;

  3. niel Avatar
    niel

    Hello, how about only display is daily post? then reset or zero after 24 hours is that possible ?

    1. Dave Warfel Avatar

      I’m sure it’s possible but I don’t have the code for it. Sorry.

  4. mithu Avatar
    mithu

    How to build a rest api endpoint that will only show total number of custom post types in number. count only.

    1. Dave Warfel Avatar

      Hi Mithu – Sorry but I don’t know how to do that.

Leave a Comment