Make WordPress Core

Ticket #43636: wp_query_fields_with_unit_tests.diff

File wp_query_fields_with_unit_tests.diff, 3.0 KB (added by mattkeys, 7 years ago)

wp_query_fields + unit tests

  • src/wp-includes/class-wp-query.php

    diff --git a/src/wp-includes/class-wp-query.php b/src/wp-includes/class-wp-query.php
    index f8ead26246..1dd105246f 100644
    a b class WP_Query { 
    18551855                        case 'id=>parent':
    18561856                                $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent";
    18571857                                break;
     1858                        case 'author_ids':
     1859                                $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_author";
     1860                                break;
     1861                        case 'titles':
     1862                                $fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_title";
     1863                                break;
    18581864                        default:
    18591865                                $fields = "{$wpdb->posts}.*";
    18601866                }
    class WP_Query { 
    29072913                        return $r;
    29082914                }
    29092915
     2916                if ( 'author_ids' == $q['fields'] ) {
     2917                        if ( null === $this->posts ) {
     2918                                $this->posts = $wpdb->get_results( $this->request );
     2919                        }
     2920
     2921                        $this->post_count = count( $this->posts );
     2922                        $this->set_found_posts( $q, $limits );
     2923
     2924                        $r = array();
     2925                        foreach ( $this->posts as $key => $post ) {
     2926                                $this->posts[ $key ]->ID = (int) $post->ID;
     2927                                $this->posts[ $key ]->post_author = (int) $post->post_author;
     2928
     2929                                $r[ (int) $post->ID ] = (int) $post->post_author;
     2930                        }
     2931
     2932                        return $r;
     2933                }
     2934
     2935                if ( 'titles' == $q['fields'] ) {
     2936                        if ( null === $this->posts ) {
     2937                                $this->posts = $wpdb->get_results( $this->request );
     2938                        }
     2939
     2940                        $this->post_count = count( $this->posts );
     2941                        $this->set_found_posts( $q, $limits );
     2942
     2943                        $r = array();
     2944                        foreach ( $this->posts as $key => $post ) {
     2945                                $this->posts[ $key ]->ID = (int) $post->ID;
     2946                                $this->posts[ $key ]->post_title = sanitize_text_field( $post->post_title );
     2947
     2948                                $r[ (int) $post->ID ] = sanitize_text_field( $post->post_title );
     2949                        }
     2950
     2951                        return $r;
     2952                }
     2953
    29102954                if ( null === $this->posts ) {
    29112955                        $split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" == $fields && ! empty( $limits ) && $q['posts_per_page'] < 500 );
    29122956
  • tests/phpunit/tests/query.php

    diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php
    index 32f3603537..e37df9308c 100644
    a b class Tests_Query extends WP_UnitTestCase { 
    685685                $this->assertSame( 'tax1', get_query_var( 'taxonomy' ) );
    686686                $this->assertSame( 'term1', get_query_var( 'term' ) );
    687687        }
     688
     689        /**
     690         * @ticket 43636
     691         */
     692        public function test_fields_author_ids_contains_only_post_ids_and_author_ids() {
     693                $user_id = self::factory()->user->create( array(
     694                        'role' => 'editor',
     695                ) );
     696
     697                $post_id = $this->factory->post->create( array( 'post_author' => $user_id ) );
     698
     699                $q = get_posts( array( 'fields' => 'author_ids' ) );
     700
     701                $this->assertEquals( $q[ $post_id ], $user_id );
     702        }
     703
     704        /**
     705         * @ticket 43636
     706         */
     707        public function test_fields_titles_contains_only_post_ids_and_titles() {
     708                $post_title     = 'Test fields titles contains only post ids and titles';
     709                $post_id        = $this->factory->post->create( array( 'post_title' => $post_title ) );
     710
     711                $q = get_posts( array( 'fields' => 'titles' ) );
     712
     713                $this->assertEquals( $q[ $post_id ], $post_title );
     714        }
     715
    688716}