Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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

any ideas

share|improve this question

4 Answers 4

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;
?>
share|improve this answer
    
I am speaking about all post types that are registered, Your code just gives count of published posts in default post type "Post" –  Jagan K Oct 11 '13 at 11:47
    
@JaganK I have edited my answer.Hope it will helpful. –  dipali Oct 11 '13 at 12:16

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;
?>
share|improve this answer
    
I am speaking about all post types that are registered, Your code just gives count of published posts in default post type "Post" –  Jagan K Oct 11 '13 at 11:47
    
hi then may this post is wat ur looking for i believe if not reply back...stackoverflow.com/questions/14067698/… –  codebreaker Oct 11 '13 at 12:06
    
That fulfills my needs, but not answers my question. So i guess there's no default function to count all post types. –  Jagan K Oct 11 '13 at 12:14
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;
}
share|improve this answer
    
Please explain your code as well as post it. –  s_ha_dum Dec 5 '13 at 14:38
    
wp_count_posts runs a query every time it is called, making that fairly heavy code, and new types would have to be added manually making it high maintenance. –  s_ha_dum Dec 5 '13 at 14:46

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);
share|improve this answer
    
Shadab is calling the publish property to get the number of post with status "publish" –  Tomás Cot Sep 16 at 15:05
    
Yes you are absolutely correct. –  Shadab Khan Sep 17 at 1:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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