使用下面的代碼,我設法獲得了每位作者的帖子數量。如何向此查詢添加類別過濾器“neuws”。為了計算“news”類別中作者的帖子數量感謝您的幫助
<?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 ();?
?>
uj5u.com熱心網友回復:
您可以使用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 );
uj5u.com熱心網友回復:
您可以通過創建自定義 WP_Query 然后對其進行計數來做到這一點。注意:您可以用 $userid 變數替換用戶的靜態 ID。
$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',
};
謝謝你,烏斯曼哈立德
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/341454.html
標籤:查询 WordPress的
上一篇:只在當前div觸發事件
