WP_Date_Query::__construct( array $date_query, string $default_column = 'post_date' )
- Since
- 3.7.0, 4.0.0, 4.1.0
- Source
wp-includes/class-wp-date-query.php:148
Description
Time-related parameters that normally require integer values ('year', 'month', 'week', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second') accept arrays of integers for some values of 'compare'. When 'compare' is 'IN' or 'NOT IN', arrays are accepted; when 'compare' is 'BETWEEN' or 'NOT BETWEEN', arrays of two valid values are required. See individual argument descriptions for accepted values.
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
$date_queryarray- Array of date query clauses. @type array ...$0 { @type string $column Optional. The column to query against. If undefined, inherits the value of the
$default_columnparameter. See WP_Date_Query::validate_column() and the 'date_query_valid_columns' filter for the list of accepted values. Default 'post_date'.$comparestringdefault: '='Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'.$relationstringOptional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'. Default 'OR'. @type array ...$0 { Optional. An array of first-order clause parameters, or another fully-formed date query.$beforestring|array{ Optional. Date to retrieve posts before. Acceptsstrtotime()-compatible string, or array of 'year', 'month', 'day' values.$yearstringdefault: empty. Accepts any four-digit yearThe four-digit year.$monthstringdefault: (string:empty)|(array:1). Accepts numbers 1-12Optional when passing array.The month of the year.$daystringOptional when passing array.The day of the month. Default (string:empty)|(array:1). Accepts numbers 1-31. } @type string|array $after { Optional. Date to retrieve posts after. Acceptsstrtotime()-compatible string, or array of 'year', 'month', 'day' values.$yearstringdefault: emptyThe four-digit year. Accepts any four-digit year.$monthstringdefault: (string:empty)|(array:12)Optional when passing array. The month of the year. Accepts numbers 1-12.$daystringdefault: is the value of top-level $columnOptional when passing array.The day of the month. Accepts numbers 1-31. Default (string:empty)|(array:last day of month). } @type string $column Optional. Used to add a clause comparing a column other than the column specified in the top-level$columnparameter. See WP_Date_Query::validate_column() and the 'date_query_valid_columns' filter for the list of accepted values.$comparestringdefault: '='Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 'IN', 'NOT IN', 'BETWEEN', and 'NOT BETWEEN'. Comparisons support arrays in some time-related parameters.$inclusivebooldefault: falseOptional. Include results from dates specified in 'before' or 'after'.$yearint|int[]default: emptyOptional. The four-digit year number. Accepts any four-digit year or an array of years if$comparesupports it.$monthint|int[]default: emptyOptional. The two-digit month number. Accepts numbers 1-12 or an array of valid numbers if$comparesupports it.$weekint|int[]default: emptyOptional. The week number of the year. Accepts numbers 0-53 or an array of valid numbers if$comparesupports it.$dayofyearint|int[]Optional. The day number of the year. Accepts numbers 1-366 or an array of valid numbers if$comparesupports it.$dayint|int[]default: emptyOptional. The day of the month. Accepts numbers 1-31 or an array of valid numbers if$comparesupports it.$dayofweekint|int[]default: emptyOptional. The day number of the week. Accepts numbers 1-7 (1 is Sunday) or an array of valid numbers if$comparesupports it.$dayofweek_isoint|int[]default: emptyOptional. The day number of the week (ISO). Accepts numbers 1-7 (1 is Monday) or an array of valid numbers if$comparesupports it.$hourint|int[]default: emptyOptional. The hour of the day. Accepts numbers 0-23 or an array of valid numbers if$comparesupports it.$minuteint|int[]default: emptyOptional. The minute of the hour. Accepts numbers 0-59 or an array of valid numbers if$comparesupports it.$secondint|int[]default: empty. } }Optional. The second of the minute. Accepts numbers 0-59 or an array of valid numbers if$comparesupports it.
$default_columnstringoptional- Default column to query against. See WP_Date_Query::validate_column() and the 'date_query_valid_columns' filter for the list of accepted values.
Default 'post_date'.Default:'post_date'
Performance profile
How much work a call to WP_Date_Query::__construct() 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
- Trivial
- Scaling
- Constant
- Instructions
- 5–48
- Plugin surface
- None
- Called by
- 4
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 56.
Nothing here hands control to plugin code.
4 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Date_Query::__construct() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 5–7 | none |
!empty($date_query) && is_array($date_query) && !isset($date_query) | 37–42 | esc_sql(), ->validate_column(), ->get_compare(), ->sanitize_query() |
!empty($date_query) && is_array($date_query) && isset($date_query) | 43–48 | ->sanitize_relation(), esc_sql(), ->validate_column(), ->get_compare(), ->sanitize_query() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 56 instructions, 5–48 executed per call, 5 branches. The work does not change between versions.
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 · 5
- esc_sql()Escapes data for use in a MySQL query.
- WP_Date_Query::sanitize_relation()Sanitizes a 'relation' operator.
- 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::sanitize_query()Recursive-friendly query sanitizer.
Used by · 4
- WP_Comment_Query::get_comment_ids()Used internally to get a list of comment IDs matching the query vars.
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Site_Query::get_site_ids()Used internally to get a list of site IDs matching the query vars.
- WP_User_Query::prepare_query()Prepares the query variables.
Source code
public function __construct( $date_query, $default_column = 'post_date' ) { if ( empty( $date_query ) || ! is_array( $date_query ) ) { return; } if ( isset( $date_query['relation'] ) ) { $this->relation = $this->sanitize_relation( $date_query['relation'] ); } else { $this->relation = 'AND'; } // Support for passing time-based keys in the top level of the $date_query array. if ( ! isset( $date_query[0] ) ) { $date_query = array( $date_query ); } if ( ! empty( $date_query['column'] ) ) { $date_query['column'] = esc_sql( $date_query['column'] ); } else { $date_query['column'] = esc_sql( $default_column ); } $this->column = $this->validate_column( $this->column ); $this->compare = $this->get_compare( $date_query ); $this->queries = $this->sanitize_query( $date_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 6.7.7 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.