wppaste
WordPress

WP_Meta_Query::get_sql_for_clause( array $clause, array $parent_query, string $clause_key = '' ): array

Since
4.1.0
Source
wp-includes/class-wp-meta-query.php:533
Generates SQL JOIN and WHERE clauses for a first-order query clause.

Description

"First-order" means that it's an array with a 'key' or 'value'.

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 (passed by reference).
$parent_queryarray
Parent query array.
$clause_keystringoptional
The array key used to name the clause in the original $meta_query parameters. If not provided, a key will be generated automatically.
Default empty string.Default: ''

Return value

array
Array containing JOIN and WHERE SQL clauses to append to a first-order query.
  • $joinstring[]

    Array of SQL fragments to append to the main JOIN clause.
  • $wherestring[]

    Array of SQL fragments to append to the main WHERE clause.

Performance profile

How much work a call to WP_Meta_Query::get_sql_for_clause() 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
69–132

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

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

What it touches

  • regexregular expression over the whole inputpreg_split()called directly

What one call costs · 11 distinct outcomes

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

WhenInstructionsCalls it makes
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && !$clause69–116->find_compatible_table_alias(), ->get_cast_for_type()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $clause81–120->find_compatible_table_alias(), ->get_cast_for_type(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare && !is_array($meta_value)84–120->find_compatible_table_alias(), ->get_cast_for_type(), preg_split()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare && !is_array($meta_value)87–124->find_compatible_table_alias(), ->get_cast_for_type(), preg_split(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key)88–122->find_compatible_table_alias(), ->get_cast_for_type(), ->esc_like(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key)91–117->find_compatible_table_alias(), ->get_cast_for_type(), str_repeat(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare && !is_array($meta_value)94–126->find_compatible_table_alias(), ->get_cast_for_type(), preg_split(), ->esc_like(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare && !is_array($meta_value)97–121->find_compatible_table_alias(), ->get_cast_for_type(), preg_split(), str_repeat(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $clause && $meta_compare !== "NOT EXISTS" && !$meta_compare_key && !$meta_compare && !is_string($meta_value)98–124->find_compatible_table_alias(), ->get_cast_for_type(), ->prepare(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $clause && $meta_compare !== "NOT EXISTS" && !$meta_compare_key && !$meta_compare && !is_string($meta_value)105–125->find_compatible_table_alias(), ->get_cast_for_type(), ->prepare(), ->esc_like(), ->prepare()
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $clause && $meta_compare !== "NOT EXISTS" && !$meta_compare_key && !$meta_compare && !is_string($meta_value)108–132->find_compatible_table_alias(), ->get_cast_for_type(), ->prepare(), str_repeat(), ->prepare()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev53669–13255
8.553669–13255
8.453669–1325522 fewer instructions than PHP 8.3
8.355869–14055
8.255869–140552 more instructions than PHP 8.1
8.155669–139557 fewer instructions than PHP 7.4
7.456369–13955

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.

Uses · 2

Used by · 1

Source code

	public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {		global $wpdb; 		$sql_chunks = array(			'where' => array(),			'join'  => array(),		); 		if ( isset( $clause['compare'] ) ) {			$clause['compare'] = strtoupper( $clause['compare'] );		} else {			$clause['compare'] = isset( $clause['value'] ) && is_array( $clause['value'] ) ? 'IN' : '=';		} 		$non_numeric_operators = array(			'=',			'!=',			'LIKE',			'NOT LIKE',			'IN',			'NOT IN',			'EXISTS',			'NOT EXISTS',			'RLIKE',			'REGEXP',			'NOT REGEXP',		); 		$numeric_operators = array(			'>',			'>=',			'<',			'<=',			'BETWEEN',			'NOT BETWEEN',		); 		if ( ! in_array( $clause['compare'], $non_numeric_operators, true ) && ! in_array( $clause['compare'], $numeric_operators, true ) ) {			$clause['compare'] = '=';		} 		if ( isset( $clause['compare_key'] ) ) {			$clause['compare_key'] = strtoupper( $clause['compare_key'] );		} else {			$clause['compare_key'] = isset( $clause['key'] ) && is_array( $clause['key'] ) ? 'IN' : '=';		} 		if ( ! in_array( $clause['compare_key'], $non_numeric_operators, true ) ) {			$clause['compare_key'] = '=';		} 		$meta_compare     = $clause['compare'];		$meta_compare_key = $clause['compare_key']; 		// First build the JOIN clause, if one is required.		$join = ''; 		// We prefer to avoid joins if possible. Look for an existing join compatible with this clause.		$alias = $this->find_compatible_table_alias( $clause, $parent_query );		if ( false === $alias ) {			$i     = count( $this->table_aliases );			$alias = $i ? 'mt' . $i : $this->meta_table; 			// JOIN clauses for NOT EXISTS have their own syntax.			if ( 'NOT EXISTS' === $meta_compare ) {				$join .= " LEFT JOIN $this->meta_table";				$join .= $i ? " AS $alias" : ''; 				if ( 'LIKE' === $meta_compare_key ) {					$join .= $wpdb->prepare( " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key LIKE %s )", '%' . $wpdb->esc_like( $clause['key'] ) . '%' );				} else {					$join .= $wpdb->prepare( " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );				} 				// All other JOIN clauses.			} else {				$join .= " INNER JOIN $this->meta_table";				$join .= $i ? " AS $alias" : '';				$join .= " ON ( $this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column )";			}

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 6.7.7 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.