Ticket #19866: 19866.2.diff
File 19866.2.diff, 2.8 KB (added by , 10 years ago) |
---|
-
tests/tests/query/results.php
369 369 'child-two', 370 370 ), wp_list_pluck( $posts, 'post_title' ) ); 371 371 } 372 373 /** 374 * @ticket 19866 375 */ 376 function test_query_fields() { 377 $posts_fields = $this->q->query( array( 378 'fields' => array( 'post_parent', 'guid' ), 379 ) ); 380 381 $post_fields = (array) reset( $posts_fields ); 382 $this->assertEqualSets( array( 'post_parent', 'guid' ), array_keys( $post_fields ) ); 383 384 $posts_ids = $this->q->query( array( 385 'fields' => 'ids', 386 ) ); 387 388 $post_ids = reset( $posts_ids ); 389 $this->assertInternalType( 'integer', $post_ids ); 390 391 $posts_id_parent = $this->q->query( array( 392 'fields' => 'id=>parent', 393 ) ); 394 395 $this->assertInternalType( 'array', $posts_id_parent ); 396 $this->assertInternalType( 'integer', reset( $posts_id_parent ) ); 397 $this->assertInternalType( 'integer', key( $posts_id_parent ) ); 398 } 372 399 } -
src/wp-includes/query.php
2048 2048 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; 2049 2049 break; 2050 2050 default: 2051 $fields = "$wpdb->posts.*"; 2051 if ( is_array( $q['fields'] ) ) { 2052 $_fields = array(); 2053 foreach ( $q['fields'] as $field ) { 2054 if ( false === strpos( $field, '.' ) ) 2055 $_fields[] = "$wpdb->posts.$field"; 2056 else 2057 $_fields[] = $field; 2058 } 2059 $fields = join( ', ', $_fields ); 2060 } else { 2061 $fields = "$wpdb->posts.*"; 2062 } 2052 2063 } 2053 2064 2054 2065 if ( '' !== $q['menu_order'] ) … … 2672 2683 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) ); 2673 2684 } 2674 2685 2675 if ( 'ids' == $q['fields'] ) { 2676 $this->posts = $wpdb->get_col( $this->request ); 2686 if ( is_array( $q['fields'] ) || in_array( $q['fields'], array( 'ids', 'id=>parent' ) ) ) { 2687 if ( 'ids' == $q['fields'] ) 2688 $this->posts = array_map( 'intval', $wpdb->get_col( $this->request ) ); 2689 else 2690 $this->posts = $wpdb->get_results( $this->request ); 2677 2691 $this->post_count = count( $this->posts ); 2678 2692 $this->set_found_posts( $q, $limits ); 2693 } 2679 2694 2695 if ( 'ids' == $q['fields'] || is_array( $q['fields'] ) ) 2680 2696 return $this->posts; 2681 }2682 2697 2683 2698 if ( 'id=>parent' == $q['fields'] ) { 2684 $this->posts = $wpdb->get_results( $this->request );2685 $this->post_count = count( $this->posts );2686 $this->set_found_posts( $q, $limits );2687 2688 2699 $r = array(); 2689 2700 foreach ( $this->posts as $post ) 2690 $r[ $post->ID ] =$post->post_parent;2701 $r[ (int) $post->ID ] = (int) $post->post_parent; 2691 2702 2692 2703 return $r; 2693 2704 }