wppaste
WordPress

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

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_column parameter. 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'.
  • $relationstring

    Optional. 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. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day' values.
  • $yearstringdefault: empty. Accepts any four-digit year

    The four-digit year.
  • $monthstringdefault: (string:empty)|(array:1). Accepts numbers 1-12

    Optional when passing array.The month of the year.
  • $daystring

    Optional 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. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day' values.
  • $yearstringdefault: empty

    The 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 $column

    Optional 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 $column parameter. 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: false

    Optional. Include results from dates specified in 'before' or 'after'.
  • $yearint|int[]default: empty

    Optional. The four-digit year number. Accepts any four-digit year or an array of years if $compare supports it.
  • $monthint|int[]default: empty

    Optional. The two-digit month number. Accepts numbers 1-12 or an array of valid numbers if $compare supports it.
  • $weekint|int[]default: empty

    Optional. The week number of the year. Accepts numbers 0-53 or an array of valid numbers if $compare supports it.
  • $dayofyearint|int[]

    Optional. The day number of the year. Accepts numbers 1-366 or an array of valid numbers if $compare supports it.
  • $dayint|int[]default: empty

    Optional. The day of the month. Accepts numbers 1-31 or an array of valid numbers if $compare supports it.
  • $dayofweekint|int[]default: empty

    Optional. The day number of the week. Accepts numbers 1-7 (1 is Sunday) or an array of valid numbers if $compare supports it.
  • $dayofweek_isoint|int[]default: empty

    Optional. The day number of the week (ISO). Accepts numbers 1-7 (1 is Monday) or an array of valid numbers if $compare supports it.
  • $hourint|int[]default: empty

    Optional. The hour of the day. Accepts numbers 0-23 or an array of valid numbers if $compare supports it.
  • $minuteint|int[]default: empty

    Optional. The minute of the hour. Accepts numbers 0-59 or an array of valid numbers if $compare supports it.
  • $secondint|int[]default: empty. } }

    Optional. The second of the minute. Accepts numbers 0-59 or an array of valid numbers if $compare supports 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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
5–48

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
4

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.

WhenInstructionsCalls it makes
always5–7none
!empty($date_query) && is_array($date_query) && !isset($date_query)37–42esc_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

Used by · 4

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.

  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.

4.1.0
Introduced 'dayofweek_iso' time type parameter.from the docblock
4.0.0
The $inclusive logic was updated to include all times within the date range.from the docblock
3.7.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.