How To Count Total Number Of WordPress Posts

Looking to display the count of the total number of WordPress posts on your blog? All you may need is to loop through the posts and auto increment a number.

Counting the total number of WordPress posts in a post type

You can easily get the total post count and filter by post type using the built-in WordPress function wp_count_posts().

Counting the total number of WordPress posts using meta values or other filters

To count the total number of WordPress posts including any specific filtration, simply add the code below to your funtions.php file, build-in your filtration and call the function in the area you’d like to display the results.

Register the function

  1. Navigate to Appearance > Editor > Theme Functions (functions.php) from your WordPress website’s dashboard;
  2. Copy and paste in the function below;
  3. Click Update File to save the changes.
function display_post_count() {
     $args = array(
          'post_type' => 'post',
          'posts_per_page' => -1,
          //include your filter, eg. meta_key, meta_value, date range, etc.
     );
     $loop = new wp_query( $args );
     $i = 0;
     while ( $loop->have_posts() ) {
          $loop->the_post();
          $i++;
     }
     wp_reset_postdata();
     return $i;
}

Usage

<?php echo display_post_count(); ?>

Note, this function can cause speed issues if you have a lot of posts to loop through.