WP_Tax_Query::get_sql_for_query( array $query, int $depth = 0 ): string[]
- Since
- 4.1.0
- Source
wp-includes/class-wp-tax-query.php:302
Description
If nested subqueries are found, this method recurses the tree to produce the properly nested SQL.
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
$queryarray- Query to parse (passed by reference).
$depthintoptional- Number of tree levels deep we currently are.
Used to calculate indentation. Default 0.Default:0
Return value
string[]- Array containing JOIN and WHERE SQL clauses to append to a single query array.
$joinstringSQL fragment to append to the main JOIN clause.$wherestringSQL fragment to append to the main WHERE clause.
Performance profile
How much work a call to WP_Tax_Query::get_sql_for_query() 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
- 30–52
- Plugin surface
- None
- Called by
- 2
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 117.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
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_Tax_Query::get_sql_for_query() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!$i && empty($sql_chunks) | 30–45 | array_filter(), array_filter() |
!$i && !empty($sql_chunks) | 37–52 | array_filter(), array_filter(), array_unique() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 117 | 30–52 | 11 | |
| 8.5 | 117 | 30–52 | 11 | |
| 8.4 | 117 | 30–52 | 11 | 9 fewer instructions than PHP 8.3 |
| 8.3 | 126 | 30–58 | 11 | |
| 8.2 | 126 | 30–58 | 11 | |
| 8.1 | 126 | 30–58 | 11 | |
| 7.4 | 126 | 30–58 | 11 |
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 · 3
- WP_Tax_Query::is_first_order_clause()Determines whether a clause is first-order.
- WP_Tax_Query::get_sql_for_clause()Generates SQL JOIN and WHERE clauses for a "first-order" query clause.
- WP_Tax_Query::get_sql_for_query()Generates SQL clauses for a single query array.
Used by · 2
- WP_Tax_Query::get_sql_clauses()Generates SQL clauses to be appended to a main query.
- WP_Tax_Query::get_sql_for_query()Generates SQL clauses for a single query array.
Source code
protected function get_sql_for_query( &$query, $depth = 0 ) { $sql_chunks = array( 'join' => array(), 'where' => array(), ); $sql = array( 'join' => '', 'where' => '', ); $indent = ''; for ( $i = 0; $i < $depth; $i++ ) { $indent .= ' '; } foreach ( $query as $key => &$clause ) { if ( 'relation' === $key ) { $relation = $query['relation']; } elseif ( is_array( $clause ) ) { // This is a first-order clause. if ( $this->is_first_order_clause( $clause ) ) { $clause_sql = $this->get_sql_for_clause( $clause, $query ); $where_count = count( $clause_sql['where'] ); if ( ! $where_count ) { $sql_chunks['where'][] = ''; } elseif ( 1 === $where_count ) { $sql_chunks['where'][] = $clause_sql['where'][0]; } else { $sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )'; } $sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] ); // This is a subquery, so we recurse. } else { $clause_sql = $this->get_sql_for_query( $clause, $depth + 1 ); $sql_chunks['where'][] = $clause_sql['where']; $sql_chunks['join'][] = $clause_sql['join']; } } } // Filter to remove empties. $sql_chunks['join'] = array_filter( $sql_chunks['join'] ); $sql_chunks['where'] = array_filter( $sql_chunks['where'] ); if ( empty( $relation ) ) { $relation = 'AND'; } // Filter duplicate JOIN clauses and combine into a single string. if ( ! empty( $sql_chunks['join'] ) ) { $sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) ); } // Generate a single WHERE clause with proper brackets and indentation. if ( ! empty( $sql_chunks['where'] ) ) { $sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')'; } return $sql; }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 6.8.8 tag, from
src/wp-includes/class-wp-tax-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.