这可以通过自定义查询完成:确保尽早挂接查询过滤器。
注意:此解决方案的帮助仅限于上面@m0r7if3r的答案,因为查询将在过滤器之前运行。
function count_all_posts_by_user_cb( $count, $userid )
{
global $wpdb;
$where = $wpdb->prepare( \'WHERE post_author = %d AND post_type = %s AND \', $userid, $wpdb->posts )
$where .= "(post_status = \'publish\'";
// for a full list, @see http://codex.wordpress.org/Post_Status_Transitions
foreach( array( \'private\', \'draft\', \'pending\', \'future\' ) as $status
$where .= $wpdb->prepare( " OR post_status = \'%s\'", $status );
$where .= ")";
$result = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
return $result;
}
add_filter( \'get_usernumposts\', \'count_all_posts_by_user_cb\', 20, 2 );
注意:筛选器将影响对的所有调用
count_user_posts();
. 如果您不想这样做,那么您必须检查过滤器cb fn内部,用户是否有特定的角色或ID,或者您需要的任何限制。