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
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_queryparameters. 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
- Scaling
- Scales with input
- Instructions
- 68–131
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 534.
Nothing here hands control to plugin code.
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 input
preg_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.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($clause) && $alias !== false && !$clause && is_int($clause_key) && !isset($clause_key) | 68–115 | ->find_compatible_table_alias(), ->get_cast_for_type() |
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) | 80–119 | ->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) | 83–119 | ->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) | 86–123 | ->find_compatible_table_alias(), ->get_cast_for_type(), preg_split(), ->prepare() |
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) | 87–121 | ->find_compatible_table_alias(), ->get_cast_for_type(), ->esc_like(), ->prepare() |
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) | 90–116 | ->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) | 93–125 | ->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) | 96–120 | ->find_compatible_table_alias(), ->get_cast_for_type(), preg_split(), str_repeat(), ->prepare() |
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare !== "NOT EXISTS" && !$meta_compare_key && !$meta_compare && !is_string($meta_value) | 97–123 | ->find_compatible_table_alias(), ->get_cast_for_type(), ->prepare(), ->prepare() |
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare !== "NOT EXISTS" && !$meta_compare_key && !$meta_compare && !is_string($meta_value) | 104–124 | ->find_compatible_table_alias(), ->get_cast_for_type(), ->prepare(), ->esc_like(), ->prepare() |
!isset($clause) && $alias !== false && is_int($clause_key) && !isset($clause_key) && $meta_compare !== "NOT EXISTS" && !$meta_compare_key && !$meta_compare && !is_string($meta_value) | 107–131 | ->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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 534 | 68–131 | 55 | |
| 8.5 | 534 | 68–131 | 55 | |
| 8.4 | 534 | 68–131 | 55 | 22 fewer instructions than PHP 8.3 |
| 8.3 | 556 | 68–139 | 55 | |
| 8.2 | 556 | 68–139 | 55 | 2 more instructions than PHP 8.1 |
| 8.1 | 554 | 68–138 | 55 | 6 fewer instructions than PHP 7.4 |
| 7.4 | 560 | 68–138 | 55 |
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
- WP_Meta_Query::find_compatible_table_alias()Identifies an existing table alias that is compatible with the current query clause.
- WP_Meta_Query::get_cast_for_type()Returns the appropriate alias for the given meta type if applicable.
Used by · 1
- WP_Meta_Query::get_sql_for_query()Generates SQL clauses for a single query array.
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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 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.