Make WordPress Core


Ignore:
Timestamp:
02/06/2025 05:02:17 AM (3 months ago)
Author:
peterwilsoncc
Message:

Query: Increase WP_Query cache hits for equivalent arguments.

Introduces normalization a number of arguments passed to WP_Query to increase cache hits for equivalent requests. For example author__in => [ 1, 2 ] and author__in => [ 2, 1 ] will now hit the same cache.

Prior to generating the SQL request and cache key, the following are sorted, made unique and type cast as appropriate.

  • post_type when passed as an array
  • post_status when passed as an array
  • term_querys containing terms
  • cat
  • category__in
  • category__not_in
  • category__and
  • tag_slug__in
  • tag__in
  • tag__not_in
  • tag__and
  • tag_slug__in
  • tag_slug__and
  • post_parent__not_in
  • author
  • author__not_in
  • author__in

The following are sorted for the purposes of generating the cache key and SQL WHERE clause but unmodified for use in the ORDER BY SQL clause:

  • post_name__in
  • post__in
  • post_parent__in

This commit includes changes to unrelated tests, assertions in Tests_Query_ParseQuery::test_parse_query_cat_array_mixed() and WP_Test_REST_Posts_Controller::test_get_items_not_sticky_with_exclude() have been modified to account for the sorting of the items above.

Props thekt12, peterwilsoncc, spacedmonkey, joemcgill, flixos90, mukesh27, pbearne, swissspidy.
Fixes #59516.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r59630 r59766  
    15751575        $this->assertNotContains( $id3, $ids );
    15761576
    1577         $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id3,$id2) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
     1577        $this->assertPostsWhere( " AND {posts}.ID NOT IN ($id2,$id3) AND {posts}.post_type = 'post' AND (({posts}.post_status = 'publish'))" );
    15781578    }
    15791579
Note: See TracChangeset for help on using the changeset viewer.