Author post count in category 时间:2021-02-27 作者:ANdy 我想统计特定类别中的作者帖子数量。我该怎么做?我把这根线染红了,但还是洗不出来。Count number of posts by author in a category编辑:这是我得到并尝试过的,但根本不起作用。$user_id = get_the_author_meta(\'ID\') $args = array( \'author_name\' => $user_id, \'category_name\' => \'categoryname\', }; $wp_query = new WP_Query($args); while ( $wp_query->have_posts() ) : $wp_query->the_post(); echo $my_count = $wp_query->post_count; wp_reset_postdata(); endwhile; 2 个回复 最合适的回答,由SO网友:ANdy 整理而成 这是正确的代码。它按类别slug名称显示特定类别的作者帖子数量。$user_id = get_the_author_meta(\'ID\'); $args = array( \'author\' => $user_id, \'category_name\' => \'category_slug_name\', ); $my_query = new WP_Query( $args ); $my_count = $my_query->post_count; echo $my_count; SO网友:Andrew Real 你不需要为此循环浏览帖子。相反,只需调用该方法get_posts() 它将返回一个帖子数组,然后只计算该数组中的帖子数。$user_id = get_the_author_meta(\'ID\'); $args = array( \'author_name\' => $user_id, \'category_name\' => \'categoryname\', ); $wp_query = new WP_Query($args); $posts = $wp_query->get_posts(); $my_count = count( $posts ); echo $my_count; 参考文献https://developer.wordpress.org/reference/classes/wp_query/get_posts/ 文章导航