是否对所有帖子类型使用WP_COUNT_POSTS?

时间:2013-10-11 作者:Jagan K

我有5种不同的自定义帖子类型,是否有任何方法可以通过任何内置函数统计所有发布的帖子,无论帖子类型如何

喜欢

wp_count_posts(array(\'post\',\'books\',\'video\'))
有什么想法吗

4 个回复
SO网友:dipali

要获取自定义帖子类型,可以调用get_post_types 作用内置的公共帖子类型包括帖子、页面和附件。通过将“\\u builtin”设置为false,我们将排除它们并仅显示自定义的公共帖子类型。下面是代码。

 <?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;
?>

SO网友:brunomarks7
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;
}
SO网友:Shadab Khan

我用于添加所有帖子

$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);

SO网友:codebreaker

试试这个,然后回复是否成功,这会给所有的猫咪帖子。。

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

结束

相关推荐