Add a $public_only argument to count_many_users_posts() and get_posts_by_author_sql(). Defaults to false, and allows the counts to be returned for only public posts. props ryan, westi. fixes #21431.
git-svn-id: http://core.svn.wordpress.org/trunk@22386 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
1fc0104667
commit
c04587d409
|
@ -4431,9 +4431,10 @@ function get_private_posts_cap_sql( $post_type ) {
|
|||
* @param string $post_type Post type.
|
||||
* @param bool $full Optional. Returns a full WHERE statement instead of just an 'andalso' term.
|
||||
* @param int $post_author Optional. Query posts having a single author ID.
|
||||
* @param bool $public_only Optional. Only return public posts. Skips cap checks for $current_user. Default is false.
|
||||
* @return string SQL WHERE code that can be added to a query.
|
||||
*/
|
||||
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null ) {
|
||||
function get_posts_by_author_sql( $post_type, $full = true, $post_author = null, $public_only = false ) {
|
||||
global $user_ID, $wpdb;
|
||||
|
||||
// Private posts
|
||||
|
@ -4457,18 +4458,21 @@ function get_posts_by_author_sql( $post_type, $full = true, $post_author = null
|
|||
|
||||
$sql .= "(post_status = 'publish'";
|
||||
|
||||
if ( current_user_can( $cap ) ) {
|
||||
// Does the user have the capability to view private posts? Guess so.
|
||||
$sql .= " OR post_status = 'private'";
|
||||
} elseif ( is_user_logged_in() ) {
|
||||
// Users can view their own private posts.
|
||||
$id = (int) $user_ID;
|
||||
if ( null === $post_author || ! $full ) {
|
||||
$sql .= " OR post_status = 'private' AND post_author = $id";
|
||||
} elseif ( $id == (int) $post_author ) {
|
||||
// Only need to check the cap if $public_only is false
|
||||
if ( false === $public_only ) {
|
||||
if ( current_user_can( $cap ) ) {
|
||||
// Does the user have the capability to view private posts? Guess so.
|
||||
$sql .= " OR post_status = 'private'";
|
||||
} elseif ( is_user_logged_in() ) {
|
||||
// Users can view their own private posts.
|
||||
$id = (int) $user_ID;
|
||||
if ( null === $post_author || ! $full ) {
|
||||
$sql .= " OR post_status = 'private' AND post_author = $id";
|
||||
} elseif ( $id == (int) $post_author ) {
|
||||
$sql .= " OR post_status = 'private'";
|
||||
} // else none
|
||||
} // else none
|
||||
} // else none
|
||||
}
|
||||
|
||||
$sql .= ')';
|
||||
|
||||
|
|
|
@ -166,9 +166,10 @@ function count_user_posts($userid) {
|
|||
*
|
||||
* @param array $users Array of user IDs.
|
||||
* @param string $post_type Optional. Post type to check. Defaults to post.
|
||||
* @param bool $public_only Optional. Only return counts for public posts. Defaults to false.
|
||||
* @return array Amount of posts each user has written.
|
||||
*/
|
||||
function count_many_users_posts( $users, $post_type = 'post' ) {
|
||||
function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
|
||||
global $wpdb;
|
||||
|
||||
$count = array();
|
||||
|
@ -176,7 +177,7 @@ function count_many_users_posts( $users, $post_type = 'post' ) {
|
|||
return $count;
|
||||
|
||||
$userlist = implode( ',', array_map( 'absint', $users ) );
|
||||
$where = get_posts_by_author_sql( $post_type );
|
||||
$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
|
||||
|
||||
$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
|
||||
foreach ( $result as $row ) {
|
||||
|
|
Loading…
Reference in New Issue