wp_count_posts on all post types?

I have 5 different custom post types, Is there any way to count all the publish posts regardless of post_type through any inbuilt functions

Like

Read More
wp_count_posts(array('post','books','video'))

any ideas

Related posts

4 comments

  1. To get the custom post types you would call the get_post_types function.The built-in public post types are post, page, and attachment. By setting ‘_builtin’ to false, we will exclude them and show only the custom public post types. Below is the code.

     <?php
    $args = array(
    'public'   => true,
    '_builtin' => false
    );
    $output = 'names'; // names or objects, note names is the default
    $operator = 'and'; // 'and' or 'or'
    $post_types = get_post_types( $args, $output, $operator ); 
    foreach ( $post_types  as $post_type ) {
       $count_posts = wp_count_posts($post_type);
       $published_posts = $published_posts+$count_posts->publish;
    }
    echo $published_posts;
    ?>
    
  2. function get_total_posts(){
        $total_posts += (int) wp_count_posts('post')->publish;
        $total_posts += (int) wp_count_posts('page')->publish;
        $total_posts += (int) wp_count_posts('custom')->publish;
        $total_posts += (int) wp_count_posts('other_type')->publish;
        return $total_posts;
    }
    
  3. I use to add all post

    $total_codes = wp_count_posts(codes);
    $total_themes = wp_count_posts('themes');
    
    
    $codes_count = $total_codes->publish;
    $themes_count = $total_themes->publish;
    $sum_total = $codes_count + $themes_count;
    
    print ($direct_text . $sum_total);
    
  4. Just try this and reply whether it worked out this will give all pusblish posts ..

    <?php 
    // Get all published pages
    $published_pages = wp_count_posts()->publish;
    echo 'Total published posts: ' . $published;
    ?>
    

Comments are closed.