skip to Main Content

In my WordPress v5.7 taxonomy.php template, I have this below code to get a custom post_type total posts count. (Code as suggested here at https://stackoverflow.com/a/66751447/3725816).

$term = get_queried_object();

$args = array(
    'post_type'      => 'your-post-type-name',
    'post_status'    => 'publish', // get only publish posts
    'posts_per_page' => -1, // get all posts
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'your-taxonomy-name',
            'field'    => 'term_id',
            'terms'    => $term->term_id
        )
    )
);

$AllPostByTerms = new WP_Query( $args );

echo $AllpostByTerms->post_count;

Now that I have multiple (many) custom post_type’s in my WordPress.

Instead of repeating the same code for each post_type in taxonomy.php template, is there a way I can get the total posts count of each custom post_type in a single function / query?


EDIT (Desired result):

I want each custom post_type total posts counts.

Example:

post-type1 = 10,
post-type-name2 = 20,
post-type-name3 = 30  

2

Answers


  1. $args = array(
    'post_type'      => ['post-type-name1', 'post-type-name2', 'post-type-name3'],
    'post_status'    => 'publish', // get only publish posts
    'posts_per_page' => -1, // get all posts
    'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'your-taxonomy-name',
            'field'    => 'term_id',
            'terms'    => $term->term_id
        )
    )
    

    );

    You can simply pass a array of all the custom post types in post type parameter

    Login or Signup to reply.
  2. So I just stumbled upon your question and not sure if you’re still interested to see an answer or not, but i’ll add my answer for future reference, just in case somebody needs it.

    So, there are multiple ways that you could achieve what you’re looking for! However there are certain things that you didn’t mention in your question!

    For example, when counting "total post counts" you didn’t mention which post status you’re referring to!

    • Counting published posts?
    • Counting draft posts?
    • Counting pending posts?
    • Counting trashed posts?
    • Counting future posts?
    • Counting private posts?

    Another thing that is not clear in your question, is the "type of posts" you’re interested to count!

    For example, in addition to your custom-post-type-1, custom-post-type-2, custom-post-type-3, wordpress has its own "built-in" post types such as:

    • Post
    • Page
    • Attachment
    • Nav_menu_item
    • Revision

    I’ll give you my answer, feel free to customize it as needed!

    Since you didn’t mention which specific "post-type" you’re looking for, i’m going to assume you’re interested only in counting your custom-post-type-1, custom-post-type-2 and custom-post-type-3 etc. Therefore, we’re going to use a wordpress function called get_post_types in order to dynamically get all of the custom post types we’re interested in. Also, I’m going to assume, you’re interested to count all of the post statuses. In order to do so, i’ll use a wordpress function called wp_count_posts.

    get_post_typesDocs

    wp_count_postsDocs

    Now using the above functions, let’s create our magical function that will return a multidimentional associative array containing the counts for each status of each custom post type!

    NO WP_QUERY NEEDED!

    function your_them_counter_post_types()
    {
    
      $args = array(
        'public'   => true,
        '_builtin' => false // This will leave out built-in post types (i.e Post, Page, Attachment, Nav_menu_item, Revision)
      );
    
      $output = 'names';
    
      $operator = 'and';
    
      $post_types = get_post_types($args, $output, $operator);
    
      $counts_array = array();
    
      if ($post_types) {
    
        if (!is_array($post_types)) {
          $post_types = (array)$post_types;
        } // just to make sure our 'foreach' doesn't throw an error!
    
        foreach ($post_types  as $post_type) {
          $current_post_type = wp_count_posts($post_type);
          $counts_array[$post_type] = array(
            'published' => intval($current_post_type->publish),
            'draft'     => intval($current_post_type->draft),
            'pending'   => intval($current_post_type->pending),
            'future'    => intval($current_post_type->future),
            'private'   => intval($current_post_type->private),
            'trash'     => intval($current_post_type->trash)
          );
        }
      }
    
      return $counts_array;
    }
    

    This will return a multidimentional associative array that looks like this:

    $counts_array
    (
        [custom-post-type-1] => Array
            (
              [published] => 10,
              [draft] => 2,
              [pending] => 3,
              [future] => 0,
              [private] => 0,
              [trash] => 1
            ),
    
        [custom-post-type-2] => Array
            (
              [published] => 20,
              [draft] => 5,
              [pending] => 4,
              [future] => 0,
              [private] => 0,
              [trash] => 2
            ),
    
        [custom-post-type-3] => Array
            (
              [published] => 30,
              [draft] => 7,
              [pending] => 1,
              [future] => 0,
              [private] => 0,
              [trash] => 3
            )
    )
    

    Now that we have a magical function, all we need to do is to call it and loop through the returned array, like so:

    $posts_counts_array = your_them_counter_post_types();
    
    foreach ($posts_counts_array as $post_type_name => $post_count_array) {
      printf(
        __('For %s: ', 'Your-Theme-text-domain'),
        $post_type_name
      );
      echo "<pre>";
      foreach ($post_count_array as $post_status_name => $post_counts) {
        printf(
          _n(
            'There is %1$s %2$s post.',
            'There are %1$s %2$s posts.',
            $post_counts,
            'Your-Theme-text-domain'
          ),
          $post_counts,
          $post_status_name
        );
        echo "<br>";
      }
      echo "</pre>";
    }
    

    This will return what you’ve asked for!

    For custom-post-type-1: 
    There are 10 published posts.
    There are 2 draft posts.
    There are 3 pending posts.
    There are 0 future posts.
    There are 0 private posts.
    There is 1 trash post.
    
    For custom-post-type-2: 
    There are 20 published posts.
    There are 5 draft posts.
    There is 1 pending post.
    There are 0 future posts.
    There are 0 private posts.
    There are 2 trash posts
    
    For custom-post-type-3: 
    There are 30 published posts.
    There are 7 draft posts.
    There is 1 pending post.
    There are 0 future posts.
    There are 0 private posts.
    There are 3 trash posts
    

    Note:
    I’ve also used the following functions to do the translations and printing the values onto the page:
    __Docs
    _nDocs
    printfDocs

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search