Ticket #17019: 17019.5.diff
File 17019.5.diff, 4.5 KB (added by , 10 years ago) |
---|
-
src/wp-admin/includes/class-wp-media-list-table.php
42 42 } 43 43 44 44 function get_views() { 45 global $ wpdb, $post_mime_types, $avail_post_mime_types;45 global $post_mime_types, $avail_post_mime_types; 46 46 47 47 $type_links = array(); 48 48 $_num_posts = (array) wp_count_attachments(); 49 49 $_total_posts = array_sum($_num_posts) - $_num_posts['trash']; 50 $total_orphans = $wpdb->get_var( "SELECT COUNT( * ) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent < 1" ); 50 51 $args = array( 52 'fields' => 'count', 53 'post_type' => 'attachment', 54 'post_status' => 'any', 55 'post_parent' => 0, 56 'suppress_filters' => false, 57 'query_context' => 'attachment_orphans_count', 58 ); 59 $total_orphans = (int) get_posts( $args ); 60 51 61 $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts)); 52 62 foreach ( $matches as $type => $reals ) 53 63 foreach ( $reals as $real ) -
src/wp-admin/includes/media.php
40 40 * @return array $tabs with gallery if post has image attachment 41 41 */ 42 42 function update_gallery_tab($tabs) { 43 global $wpdb;44 45 43 if ( !isset($_REQUEST['post_id']) ) { 46 44 unset($tabs['gallery']); 47 45 return $tabs; … … 49 47 50 48 $post_id = intval($_REQUEST['post_id']); 51 49 52 if ( $post_id ) 53 $attachments = intval( $wpdb->get_var( $wpdb->prepare( "SELECT count(*) FROM $wpdb->posts WHERE post_type = 'attachment' AND post_status != 'trash' AND post_parent = %d", $post_id ) ) ); 50 if ( $post_id ) { 51 $args = array( 52 'fields' => 'count', 53 'post_type' => 'attachment', 54 'post_status' => 'any', 55 'post_parent' => $post_id, 56 'suppress_filters' => false, 57 'query_context' => 'gallery_attachment_count', 58 ); 59 $attachments = (int) get_posts( $args ); 60 } 54 61 55 62 if ( empty($attachments) ) { 56 63 unset($tabs['gallery']); -
src/wp-includes/query.php
1424 1424 , 'sentence' 1425 1425 , 'fields' 1426 1426 , 'menu_order' 1427 , 'query_context' 1427 1428 ); 1428 1429 1429 1430 foreach ( $keys as $key ) { … … 2272 2273 case 'id=>parent': 2273 2274 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; 2274 2275 break; 2276 case 'count': 2277 $fields = "COUNT(*)"; 2278 break; 2275 2279 default: 2276 2280 $fields = "$wpdb->posts.*"; 2277 2281 } … … 2925 2929 return $r; 2926 2930 } 2927 2931 2932 if ( 'count' == $q['fields'] ) { 2933 $this->posts = array(); 2934 $this->post_count = $wpdb->get_var( $this->request ); 2935 return $this->post_count; 2936 } 2937 2928 2938 $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 ); 2929 2939 $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this ); 2930 2940 -
tests/phpunit/tests/query.php
78 78 } 79 79 80 80 } 81 82 function test_fields_count() { 83 $this->factory->post->create_many( 3 ); 84 85 $q = new WP_Query( array( 'fields' => 'count' ) ); 86 $this->assertNotEquals( 3, $q->posts ); 87 $this->assertEquals( 3, $q->post_count ); 88 89 $p = get_posts( array( 'fields' => 'count' ) ); 90 $this->assertEquals( 3, $p ); 91 } 92 93 function test_query_context() { 94 $this->factory->post->create_many( 5 ); 95 96 $q = new WP_Query( array( 'post_type' => 'post' ) ); 97 $this->assertEquals( 5, $q->post_count ); 98 99 add_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts' ) ); 100 101 $q = new WP_Query( array( 'query_context' => 'burrito' ) ); 102 $this->assertEquals( 3, $q->post_count ); 103 104 $q = new WP_Query( array( 'query_context' => 'taco' ) ); 105 $this->assertEquals( 5, $q->post_count ); 106 107 remove_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts' ) ); 108 109 $q = new WP_Query( array( 'query_context' => 'burrito' ) ); 110 $this->assertEquals( 5, $q->post_count ); 111 } 112 113 function filter_pre_get_posts( &$query ) { 114 if ( 'burrito' === $query->get( 'query_context' ) ) { 115 $query->set( 'posts_per_page', 3 ); 116 } 117 } 81 118 } 119 No newline at end of file