【问题标题】:how to calculate the number of posts in a category and by author如何计算一个类别和作者的帖子数量
【发布时间】:2021-12-14 12:32:15
【问题描述】:

使用下面的代码, 我设法获得每位作者的帖子数量。 如何向此查询添加类别过滤器“neuws”。 为了计算“新闻”类别中作者的帖子数量 感谢您的帮助

<?php
//pour voir les journees de peches de mebres en ayant deja postees et le nombre de journees
// 1. We define the arguments to define what we want to recover
$args = array (
    'post_type' => 'post',
    'posts_per_page' => '16',
    
);

// 2. We run the WP Query
// The Query
$the_query = new WP_Query ($args);

// 3. we display the number of messages and the authors!
// The Loop
if ($the_query-> have_posts()) {
    //Set arguments to grab all authors with published peches, and order them by user ID
    $authorArgs = array(
        'orderby' => 'ID',
        'has_published_posts' => array('post',),
        
    );

    //Create an array of all authors with peches published
    $pecheAuthors = get_users($authorArgs);

    //Loop through each peche author
    foreach($pecheAuthors as $user){
        //Output user post count for peches
        echo'<font color="#f00;"><b>';
        echo count_user_posts($user->ID, 'post');
        echo'</b></font>';
        echo ' journees de pêches pour ';

        //Output user's display name
        echo'<font color="#f00;"><b>';
        echo $user->display_name;
        echo'</b></font>';
        echo '<br />';
    }

    // 3. We launch the loop to display the articles and the authors!
    // The Loop
    
} else {
    // no posts found
}
//wp_reset_postdata ();?
?>

【问题讨论】:

    标签: jquery wordpress


    【解决方案1】:

    您可以使用get_usernumposts 过滤器挂钩。试试下面的代码。代码将进入您的活动主题 functions.php 文件。

    function count_author_post_by_category( $count, $userid, $post_type, $public_only ){
    
        $author_posts = new WP_Query(array(
            'author'         => $userid,
            'posts_per_page' => -1,
            'tax_query'      => array(
                array(
                    'taxonomy' => 'category',
                    'field'    => 'slug',
                    'terms'    => 'neuws', // your category slug.
                )
            )
        ));
    
        if( $author_posts->have_posts() ) {
            $count = $author_posts->found_posts;
        }
    
        return $count;
    
    }
    add_filter( 'get_usernumposts', 'count_author_post_by_category', 10, 4 );
    

    【讨论】:

    • 感谢您的代码,但我希望能够在我的网站页面中显示它
    • 你想在哪里显示?
    • 感谢您的帮助。如果可能,请在我的主页上。但最好的办法是能够将它集成到一个 shordcode 中,这样我就可以在任何我想要的地方显示它
    • 我已经回答了您提出的原始问题。您现在要求的另一个要求是
    • 我的请求没有改变。我想按作者显示新闻类别的出版物数量。像这样,作者 A 的 13 个“新消息”类别的新消息 22 个作者 B 的“新消息”类别的新消息 47 个作者 C 的“新消息”类别的新消息 ...
    【解决方案2】:

    您可以通过创建自定义 WP_Query 然后对其进行计数来做到这一点。 注意:您可以将用户的静态 ID 替换为您的 $userid 变量。

    $args = array( 
       'author' => 1,
       'cat'    => 5,
    );
    
    $my_query = new WP_Query( $args );
    $my_count = $my_query->post_count;
    

    或者,您也可以使用类别 slug 或作者的好听的名字(不是名字!):

    $args = array(
       'author_name'   => 'bob',
       'category_name' => 'editorial',
    };
    

    谢谢你, 乌斯曼·哈立德

    【讨论】:

    • 感谢您的代码,但在这种情况下,我只会得到单个用户的结果我想知道每个用户的新类别的帖子数
    猜你喜欢
    • 1970-01-01
    • 2012-03-30
    • 2018-12-24
    • 1970-01-01
    • 2022-11-22
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 2011-03-07
    相关资源
    最近更新 更多