perf(2meet-data-optimizer): route HivePress's featured-listing lookup

- HivePress runs it on every listing search page from its own
  pre_get_posts: get_posts() with the search's meta query (still unrouted,
  including its sort group), meta_key hp_featured and orderby rand. The
  router declined both shapes, so the lookup stayed native: 2.26–2.38 s of a
  2.5 s page at 1.3M listings on dev36
- a sort group (key NOT EXISTS OR key EXISTS) that no orderby uses matches
  every post and is dropped; meta_key without meta_value and without a
  meta_value sort routes as EXISTS (WP_Query turns it into that clause)
- Verify: PostQueryRouterTest (featured lookup routed, meta_key with a value
  untouched), integration 592, unit 704, phpcs, stan; dev36 front-end
- PR: PR-122 (follow-up)
This commit is contained in:
2026-09-27 22:54:17 +08:00
parent 2ed48c3ba3
commit 66db9e4c56
2 changed files with 67 additions and 2 deletions
@@ -191,6 +191,25 @@ class TMDO_Post_Query_Router {
$routed[ $pt ][] = $route;
$sort = self::sort_for( $pt, $route, $direction );
}
// meta_key alone (no meta_value, not a meta_value sort): WP_Query adds a
// clause that tests the key exists — HivePress's featured-listing lookup.
$exists_key = ! $take_key && is_string( $meta_key ) && '' !== $meta_key
&& '' === (string) $query->get( 'meta_value' ) && '' === (string) $query->get( 'meta_compare' )
&& ! in_array( $sort_name, array( 'meta_value', 'meta_value_num' ), true )
&& null !== self::field_for( array( 'key' => $meta_key ), $pt );
if ( $exists_key ) {
$route = self::route_clause(
array(
'key' => $meta_key,
'compare' => 'EXISTS',
),
self::field_for( array( 'key' => $meta_key ), $pt )
);
if ( ! $route ) {
return;
}
$routed[ $pt ][] = $route;
}
foreach ( $raw_meta_query as $k => $clause ) {
if ( 'relation' === $k || ! is_array( $clause ) ) {
@@ -199,7 +218,10 @@ class TMDO_Post_Query_Router {
}
if ( ! isset( $clause['key'] ) ) {
// Sort shape: matches every post, only orders by the key.
$name = 'AND' === $relation ? self::sort_shape( $clause ) : '';
$name = 'AND' === $relation ? self::sort_shape( $clause ) : '';
if ( '' !== $name && ! in_array( $name, $named, true ) && self::field_for( $clause[ $name ], $pt ) ) {
continue; // Matches every post and orders nothing here (a copy of a search's meta query).
}
$field = '' !== $name && $name === $sort_name ? self::field_for( $clause[ $name ], $pt ) : null;
if ( $field ) {
$route = self::route_clause( array( 'compare' => 'EXISTS' ) + $clause[ $name ], $field );
@@ -239,7 +261,7 @@ class TMDO_Post_Query_Router {
if ( empty( $routed ) && empty( $sort ) ) {
return;
}
if ( $take_key ) {
if ( $take_key || $exists_key ) {
$query->set( 'meta_key', '' );
}
if ( $sort ) {