Make WordPress Core

Ticket #19866: 19866.2.diff

File 19866.2.diff, 2.8 KB (added by wonderboymusic, 10 years ago)
  • tests/tests/query/results.php

     
    369369                        'child-two',
    370370                ), wp_list_pluck( $posts, 'post_title' ) );
    371371        }
     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        }
    372399}
  • src/wp-includes/query.php

     
    20482048                                $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
    20492049                                break;
    20502050                        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                                }
    20522063                }
    20532064
    20542065                if ( '' !== $q['menu_order'] )
     
    26722683                        $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this ) );
    26732684                }
    26742685
    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 );
    26772691                        $this->post_count = count( $this->posts );
    26782692                        $this->set_found_posts( $q, $limits );
     2693                }
    26792694
     2695                if ( 'ids' == $q['fields'] || is_array( $q['fields'] ) )
    26802696                        return $this->posts;
    2681                 }
    26822697
    26832698                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 
    26882699                        $r = array();
    26892700                        foreach ( $this->posts as $post )
    2690                                 $r[ $post->ID ] = $post->post_parent;
     2701                                $r[ (int) $post->ID ] = (int) $post->post_parent;
    26912702
    26922703                        return $r;
    26932704                }