这是我编写的一个自定义函数,它允许您按特定作者的帖子类型和帖子状态进行查询。您可能需要小心传递的参数,因为它可能包括自动草稿和修订。。。
以下内容改编自get_posts_by_author_sql
function my_count_posts_by_user($post_author=null,$post_type=array(),$post_status=array()) {
global $wpdb;
if(empty($post_author))
return 0;
$post_status = (array) $post_status;
$post_type = (array) $post_type;
$sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND ", $post_author );
//Post status
if(!empty($post_status)){
$argtype = array_fill(0, count($post_status), \'%s\');
$where = "(post_status=".implode( " OR post_status=", $argtype).\') AND \';
$sql .= $wpdb->prepare($where,$post_status);
}
//Post type
if(!empty($post_type)){
$argtype = array_fill(0, count($post_type), \'%s\');
$where = "(post_type=".implode( " OR post_type=", $argtype).\') AND \';
$sql .= $wpdb->prepare($where,$post_type);
}
$sql .=\'1=1\';
$count = $wpdb->get_var($sql);
return $count;
}