diff --git a/wp-includes/post.php b/wp-includes/post.php index 024afbdd0c..eecdd7bf91 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -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 .= ')'; diff --git a/wp-includes/user.php b/wp-includes/user.php index 187a9b6b05..7bdc37cc76 100644 --- a/wp-includes/user.php +++ b/wp-includes/user.php @@ -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 ) {