WP_Date_Query::get_sql_for_clause( array $query, array $parent_query ): array
- Since
- 4.1.0
- Source
wp-includes/class-wp-date-query.php:721
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- Date query clause.
$parent_queryarray- Parent query of the current date query.
Return value
array- Array containing JOIN and WHERE SQL clauses to append to the main 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_Date_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
- Heavy
- Scaling
- Scales with input
- Instructions
- 36–98
- Plugin surface
- None
- Called by
- 2
Reads stored settings via get_option(), cached per request but not free on a cold cache.
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 155.
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 it touches
- optionoption read or write
get_option()one call below WP_Date_Query::get_sql_for_clause()
Further down the call graph this can also reach hook, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 12 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Date_Query::get_sql_for_clause() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($query) && !isset($query) | 36–39 | ->validate_column(), ->get_compare() |
!isset($query) | 42–45 | esc_sql(), ->validate_column(), ->get_compare() |
empty($query) && isset($query) | 49–59 | ->validate_column(), ->get_compare(), ->build_time_query() |
!isset($query) | 52–56 | ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare() |
isset($query) | 55–65 | esc_sql(), ->validate_column(), ->get_compare(), ->build_time_query() |
!isset($query) | 58–62 | esc_sql(), ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare() |
isset($query) | 65–76 | ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare(), ->build_time_query() |
!isset($query) | 69–72 | ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare(), ->build_mysql_datetime(), ->prepare() |
isset($query) | 71–82 | esc_sql(), ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare(), ->build_time_query() |
!empty($query) && !isset($query) | 75–78 | esc_sql(), ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare(), ->build_mysql_datetime(), ->prepare() |
isset($query) | 82–92 | ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare(), ->build_mysql_datetime(), ->prepare(), ->build_time_query() |
!empty($query) && isset($query) | 88–98 | esc_sql(), ->validate_column(), ->get_compare(), ->build_mysql_datetime(), ->prepare(), ->build_mysql_datetime(), ->prepare(), ->build_time_query() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 154 | 36–97 | 20 | 1 fewer instruction than PHP 8.5 |
| 8.5 | 155 | 36–98 | 20 | |
| 8.4 | 155 | 36–98 | 20 | |
| 8.3 | 155 | 36–98 | 20 | |
| 8.2 | 155 | 36–98 | 20 | 1 more instruction than PHP 8.1 |
| 8.1 | 154 | 36–98 | 20 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 155 | 37–98 | 20 |
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 · 7
- esc_sql()Escapes data for use in a MySQL query.
- _wp_mysql_week()Returns a MySQL expression for selecting the week number based on the start_of_week option.
- WP_Date_Query::validate_column()Validates a column name parameter.
- WP_Date_Query::get_compare()Determines and validates what comparison operator to use.
- WP_Date_Query::build_mysql_datetime()Builds a MySQL format date/time based on some query parameters.
- WP_Date_Query::build_value()Builds and validates a value string based on the comparison operator.
- WP_Date_Query::build_time_query()Builds a query string for comparing time values (hour, minute, second).
Used by · 2
- WP_Date_Query::get_sql_for_query()Generates SQL clauses for a single query array.
- WP_Date_Query::get_sql_for_subquery()Turns a single date clause into pieces for a WHERE clause.
Source code
protected function get_sql_for_clause( $query, $parent_query ) { global $wpdb; // The sub-parts of a $where part. $where_parts = array(); $column = ( ! empty( $query['column'] ) ) ? esc_sql( $query['column'] ) : $this->column; $column = $this->validate_column( $column ); $compare = $this->get_compare( $query ); $inclusive = ! empty( $query['inclusive'] ); // Assign greater- and less-than values. $lt = '<'; $gt = '>'; if ( $inclusive ) { $lt .= '='; $gt .= '='; } // Range queries. if ( ! empty( $query['after'] ) ) { $where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) ); } if ( ! empty( $query['before'] ) ) { $where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) ); } // Specific value queries. $date_units = array( 'YEAR' => array( 'year' ), 'MONTH' => array( 'month', 'monthnum' ), '_wp_mysql_week' => array( 'week', 'w' ), 'DAYOFYEAR' => array( 'dayofyear' ), 'DAYOFMONTH' => array( 'day' ), 'DAYOFWEEK' => array( 'dayofweek' ), 'WEEKDAY' => array( 'dayofweek_iso' ), ); // Check of the possible date units and add them to the query. foreach ( $date_units as $sql_part => $query_parts ) { foreach ( $query_parts as $query_part ) { if ( isset( $query[ $query_part ] ) ) { $value = $this->build_value( $compare, $query[ $query_part ] ); if ( $value ) { switch ( $sql_part ) { case '_wp_mysql_week': $where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; break; case 'WEEKDAY': $where_parts[] = "$sql_part( $column ) + 1 $compare $value"; break; default: $where_parts[] = "$sql_part( $column ) $compare $value"; } break; } } } } if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) { // Avoid notices. foreach ( array( 'hour', 'minute', 'second' ) as $unit ) { if ( ! isset( $query[ $unit ] ) ) { $query[ $unit ] = null; } } $time_query = $this->build_time_query( $column, $compare, $query['hour'], $query['minute'], $query['second'] ); if ( $time_query ) { $where_parts[] = $time_query; } } /*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.1.0 tag, from
src/wp-includes/class-wp-date-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.