wppaste
WordPress

WP_Meta_Query::find_compatible_table_alias( array $clause, array $parent_query ): string|false

Since
4.1.0
Source
wp-includes/class-wp-meta-query.php:830
Identifies an existing table alias that is compatible with the current query clause.

Description

We avoid unnecessary table joins by allowing each clause to look for an existing table alias that is compatible with the query that it needs to perform.

An existing alias is compatible if (a) it is a sibling of $clause (ie, it's under the scope of the same relation), and (b) the combination of operator and relation between the clauses allows for a shared table join.
In the case of WP_Meta_Query, this only applies to 'IN' clauses that are connected by the relation 'OR'.

Compatibility

WordPress
since 4.1.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$clausearray
Query clause.
$parent_queryarray
Parent query of $clause.

Return value

string|false
Table alias if found, otherwise false.

Performance profile

How much work a call to WP_Meta_Query::find_compatible_table_alias() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
14–55

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 57.

Plugin surface
1 hook

Third-party callbacks on 'meta_query_find_compatible_table_alias' run inside this call, and their cost is not bounded by anything here.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()called directly

What one call costs · 2 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Meta_Query::find_compatible_table_alias() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always14–15apply_filters()
!$parent_query && !empty($sibling) && is_array($sibling) && ->is_first_order_clause() && $clause_compare && $sibling_compare48–55->is_first_order_clause(), strtoupper(), strtoupper(), apply_filters()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5714–5511
8.55714–5511
8.45714–551110 fewer instructions than PHP 8.3
8.36714–6511
8.26714–6511
8.16714–6511
7.46714–6511

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Meta_Query::find_compatible_table_alias() runs, in this order:

  1. apply_filters( meta_query_find_compatible_table_alias )filterline 873 (+43 into the body)

    Filters the table alias identified as compatible with the current clause.

Uses · 2

Used by · 1

Source code

	protected function find_compatible_table_alias( $clause, $parent_query ) {		$alias = false; 		foreach ( $parent_query as $sibling ) {			// If the sibling has no alias yet, there's nothing to check.			if ( empty( $sibling['alias'] ) ) {				continue;			} 			// We're only interested in siblings that are first-order clauses.			if ( ! is_array( $sibling ) || ! $this->is_first_order_clause( $sibling ) ) {				continue;			} 			$compatible_compares = array(); 			// Clauses connected by OR can share joins as long as they have "positive" operators.			if ( 'OR' === $parent_query['relation'] ) {				$compatible_compares = array( '=', 'IN', 'BETWEEN', 'LIKE', 'REGEXP', 'RLIKE', '>', '>=', '<', '<=' ); 				// Clauses joined by AND with "negative" operators share a join only if they also share a key.			} elseif ( isset( $sibling['key'] ) && isset( $clause['key'] ) && $sibling['key'] === $clause['key'] ) {				$compatible_compares = array( '!=', 'NOT IN', 'NOT LIKE' );			} 			$clause_compare  = strtoupper( $clause['compare'] );			$sibling_compare = strtoupper( $sibling['compare'] );			if ( in_array( $clause_compare, $compatible_compares, true ) && in_array( $sibling_compare, $compatible_compares, true ) ) {				$alias = preg_replace( '/\W/', '_', $sibling['alias'] );				break;			}		} 		/**		 * Filters the table alias identified as compatible with the current clause.		 *		 * @since 4.1.0		 *		 * @param string|false  $alias        Table alias, or false if none was found.		 * @param array         $clause       First-order query clause.		 * @param array         $parent_query Parent of $clause.		 * @param WP_Meta_Query $query        WP_Meta_Query object.		 */		return apply_filters( 'meta_query_find_compatible_table_alias', $alias, $clause, $parent_query, $this );	}

Changelog

Introduced in 4.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/class-wp-meta-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.