Make WordPress Core

Changeset 53485


Ignore:
Timestamp:
06/10/2022 03:45:45 PM (22 months ago)
Author:
spacedmonkey
Message:

REST API: Improve post cache priming in WP_REST_Post_Search_Handler class.

In the WP_REST_Post_Search_Handler class, ensure that post, post meta and term caches are correctly primed when performing a search.

Props furi3r, spacedmonkey, TimothyBlynJacobs, audrasjb, peterwilsoncc.
Fixes #55674.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/search/class-wp-rest-post-search-handler.php

    r49955 r53485  
    5959
    6060        $query_args = array(
    61             'post_type'           => $post_types,
    62             'post_status'         => 'publish',
    63             'paged'               => (int) $request['page'],
    64             'posts_per_page'      => (int) $request['per_page'],
    65             'ignore_sticky_posts' => true,
    66             'fields'              => 'ids',
     61            'post_type'              => $post_types,
     62            'post_status'            => 'publish',
     63            'paged'                  => (int) $request['page'],
     64            'posts_per_page'         => (int) $request['per_page'],
     65            'ignore_sticky_posts'    => true,
    6766        );
    6867
     
    8483
    8584        $query     = new WP_Query();
    86         $found_ids = $query->query( $query_args );
     85        $posts     = $query->query( $query_args );
     86        // Querying the whole post object will warm the object cache, avoiding an extra query per result.
     87        $found_ids = wp_list_pluck( $posts, 'ID' );
    8788        $total     = $query->found_posts;
    8889
  • trunk/tests/phpunit/tests/rest-api/rest-search-controller.php

    r51367 r53485  
    334334
    335335    /**
     336     * @ticket 55674
     337     */
     338    public function test_get_items_search_prime_ids() {
     339        $action = new MockAction();
     340        add_filter( 'query', array( $action, 'filter' ), 10, 2 );
     341
     342        $query_args = array(
     343            'per_page' => 100,
     344            'search'   => 'foocontent',
     345        );
     346        $response   = $this->do_request_with_params( $query_args );
     347        $this->assertSame( 200, $response->get_status(), 'Request Status Response is not 200.' );
     348
     349        $ids = wp_list_pluck( $response->get_data(), 'id' );
     350        $this->assertSameSets( self::$my_content_post_ids, $ids, 'Query result posts ids do not match with expected ones.' );
     351
     352        $args               = $action->get_args();
     353        $primed_query_found = false;
     354        foreach ( $args as $arg ) {
     355            // Primed query will use WHERE ID IN clause.
     356            if ( str_contains( $arg[0], 'WHERE ID IN (' . implode( ',', $ids ) ) ) {
     357                $primed_query_found = true;
     358                break;
     359            }
     360        }
     361
     362        $this->assertTrue( $primed_query_found, 'Prime query was not executed.' );
     363    }
     364
     365    /**
    336366     * Test retrieving a single item isn't possible.
    337367     */
Note: See TracChangeset for help on using the changeset viewer.