Ticket #21364: 21364.diff
File 21364.diff, 2.3 KB (added by , 10 years ago) |
---|
-
src/wp-includes/user.php
250 250 * Number of posts user has written. 251 251 * 252 252 * @since 3.0.0 253 * @since 4.0.0 Added $post_type parameter. 253 254 * 254 255 * @global wpdb $wpdb WordPress database object for queries. 255 256 * 256 * @param int $userid User ID. 257 * @return int Amount of posts user has written. 257 * @param int $userid User ID. 258 * @param string $post_type Optional. Post type to count the number of posts for. Default 'post'. 259 * @return int Number of posts the user has written in this post type. 258 260 */ 259 function count_user_posts( $userid) {261 function count_user_posts( $userid, $post_type = 'post' ) { 260 262 global $wpdb; 261 263 262 $where = get_posts_by_author_sql( 'post', true, $userid);264 $where = get_posts_by_author_sql( $post_type, true, $userid ); 263 265 264 266 $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" ); 265 267 … … 267 269 * Filter the number of posts a user has written. 268 270 * 269 271 * @since 2.7.0 272 * @since 4.0.0 Added $post_type parameter. 270 273 * 271 * @param int $count The user's post count. 272 * @param int $userid User ID. 274 * @param int $count The user's post count. 275 * @param int $userid User ID. 276 * @param string $post_type Post type to count the number of posts for. 273 277 */ 274 return apply_filters( 'get_usernumposts', $count, $userid );278 return apply_filters( 'get_usernumposts', $count, $userid, $post_type ); 275 279 } 276 280 277 281 /** -
tests/phpunit/tests/user.php
616 616 } 617 617 618 618 /** 619 * @ticket 21364 620 */ 621 function test_count_user_posts() { 622 $post_type = rand_str( 10 ); 623 register_post_type( $post_type ); 624 625 $user_id = $this->factory->user->create( array( 'role' => 'author' ) ); 626 627 wp_set_current_user( $user_id ); 628 $this->factory->post->create_many( 15, array( 629 'post_author' => $user_id, 630 'post_type' => $post_type 631 ) ); 632 633 $count = count_user_posts( $user_id, $post_type ); 634 $this->assertEquals( 15, $count ); 635 } 636 637 /** 619 638 * @ticket 22858 620 639 */ 621 640 function test_wp_update_user_on_nonexistent_users() {