按作者统计自定义帖子类型中的帖子

时间:2015-08-03 作者:sgspragg

我已经用谷歌搜索了一些不同的方法,但找不到我正在寻找的答案。我想能够统计每个作者在自定义帖子类型中的帖子数量。如果您有任何建议,我们将不胜感激

4 个回复
最合适的回答,由SO网友:terminator 整理而成

我在wordpress中找到了这个代码。组织。此代码将在作者模板中工作。

<?php
global $wp_query;
$curauth = $wp_query->get_queried_object();
$post_count = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = \'" . $curauth->ID . "\' AND post_type = \'post\' AND post_status = \'publish\'");
?>
<h2>Post Count: <?php echo $post_count; ?></h2>
但如果您希望在其他地方使用,请更换$curauth->ID 具有作者id和post 使用所需的帖子类型

它应该会起作用。祝你一切顺利

SO网友:Abouasy
<?php
// get author ID
$author_id = get_the_author_meta( \'ID\' ); 

// echo count for post type (post and book)
echo  count_user_posts( $author_id , [\'post\',\'book\']  ); 

?>
SO网友:inrsaurabh

 if you\'re on a single post, it will return the post object
    if you\'re on a page, it will return the page object
    if you\'re on an archive page, it will return the post type object
    if you\'re on a category archive, it will return the category object
    if you\'re on an author archive, it will return the author object
etc.
使用query-object 按照@terminator的建议

代替使用sql查询使用count_user_posts

  $queried_object = get_queried_object();
    count_user_posts($queried_object->ID, \'custom_post_type_here\');

SO网友:kwiz

在中创建函数functions.php

 function count_posts_by_author($post_type){
   global $wp_query;
   $curauth = $wp_query->get_queried_object();
   $args = array(
     \'post_type\' => $post_type,
     \'author\'        =>  $curauth->ID,
     \'posts_per_page\' => -1 // no limit
   );
   echo count(get_posts($args));
 }
只需调用模板文件中的函数

<?php count_posts_by_author(\'Your post type or custom post type\');?>

结束

相关推荐

如何使用$Counter变量将帖子拉入具有不同图像大小的两列中?

我有一个自定义模块,它将一个特定类别的6个帖子显示为2列的特色图像。我喜欢每列顶部的第一篇文章显示一个大图像,如下图所示:我已经为第一列和第二列编写了必要的CSS代码,但不知道如何正确使用$计数器变量才能正确显示此模块。<?php $td_query = new WP_Query( $td_args ); $counter = 1; if ( $td_query->have_posts() ) { while (