WP_Date_Query::__construct() WordPress Method

The WP_Date_Query::__construct() method sets up the date query with default values. It accepts an array of args, which can be used to override the default values. The default values are: 'year' => date( 'Y' ), 'month' => date( 'm' ), 'day' => date( 'd' ), 'hour' => date( 'H' ), 'minute' => date( 'i' ), 'second' => date( 's' ), 'min_year' => null, 'max_year' => null, 'min_date' => null, 'max_date' => null, 'inclusive' => true, The WP_Date_Query::__construct() method sets up the date query with default values. It accepts an array of args, which can be used to override the default values. The default values are: 'year' => date( 'Y' ), 'month' => date( 'm' ), 'day' => date( 'd' ), 'hour' => date( 'H' ), 'minute' => date( 'i' ), 'second' => date( 's' ), 'min_year' => null, 'max_year' => null, 'min_date' => null, 'max_date' => null, 'inclusive' => true, The args can be used to set the date query to a specific time period. For example, to set the date query to January 1st, 2020 to March 31st, 2020, you would use the following args: array( 'year' => 2020, 'month' => array( 1, 3 ), 'day' => 1, 'inclusive' => true, ) The 'inclusive' arg is used to specify whether the start and end dates should be included in the query. If set to false, the query will return results for January 1st, 2020 to March 30th, 2020.

WP_Date_Query::__construct( array $date_query, string $default_column = 'post_date' ) #

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.


Top ↑

Parameters

$date_query

(array)(Required)Array of date query clauses.

  • '...$0'
    (array)
    • 'column'
      (string) 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'.
    • 'compare'
      (string) Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default '='.
    • 'relation'
      (string) Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'. Default 'OR'.
    • '...$0'
      (array) Optional. An array of first-order clause parameters, or another fully-formed date query.
      • 'before'
        (string|array) Optional. Date to retrieve posts before. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day' values.
        • 'year'
          (string) The four-digit year. Default empty. Accepts any four-digit year.
        • 'month'
          (string) Optional when passing array.The month of the year. Default (string:empty)|(array:1). Accepts numbers 1-12.
        • 'day'
          (string) Optional when passing array.The day of the month. Default (string:empty)|(array:1). Accepts numbers 1-31.
      • 'after'
        (string|array) Optional. Date to retrieve posts after. Accepts strtotime()-compatible string, or array of 'year', 'month', 'day' values.
        • 'year'
          (string) The four-digit year. Accepts any four-digit year. Default empty.
        • 'month'
          (string) Optional when passing array. The month of the year. Accepts numbers 1-12. Default (string:empty)|(array:12).
        • 'day'
          (string) Optional when passing array.The day of the month. Accepts numbers 1-31. Default (string:empty)|(array:last day of month).
      • 'column'
        (string) 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. Default is the value of top-level $column.
      • 'compare'
        (string) 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. Default '='.
      • 'inclusive'
        (bool) Optional. Include results from dates specified in 'before' or 'after'. Default false.
      • 'year'
        (int|int[]) Optional. The four-digit year number. Accepts any four-digit year or an array of years if $compare supports it. Default empty.
      • 'month'
        (int|int[]) Optional. The two-digit month number. Accepts numbers 1-12 or an array of valid numbers if $compare supports it. Default empty.
      • 'week'
        (int|int[]) Optional. The week number of the year. Accepts numbers 0-53 or an array of valid numbers if $compare supports it. Default empty.
      • 'dayofyear'
        (int|int[]) Optional. The day number of the year. Accepts numbers 1-366 or an array of valid numbers if $compare supports it.
      • 'day'
        (int|int[]) Optional. The day of the month. Accepts numbers 1-31 or an array of valid numbers if $compare supports it. Default empty.
      • 'dayofweek'
        (int|int[]) Optional. The day number of the week. Accepts numbers 1-7 (1 is Sunday) or an array of valid numbers if $compare supports it. Default empty.
      • 'dayofweek_iso'
        (int|int[]) 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. Default empty.
      • 'hour'
        (int|int[]) Optional. The hour of the day. Accepts numbers 0-23 or an array of valid numbers if $compare supports it. Default empty.
      • 'minute'
        (int|int[]) Optional. The minute of the hour. Accepts numbers 0-59 or an array of valid numbers if $compare supports it. Default empty.
      • 'second'
        (int|int[]) Optional. The second of the minute. Accepts numbers 0-59 or an array of valid numbers if $compare supports it. Default empty. }

$default_column

(string)(Optional) 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 value: 'post_date'


Top ↑

Source

File: wp-includes/class-wp-date-query.php

	public function __construct( $date_query, $default_column = 'post_date' ) {
		if ( empty( $date_query ) || ! is_array( $date_query ) ) {
			return;
		}

		if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) {
			$this->relation = 'OR';
		} 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 );
	}


Top ↑

Changelog

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

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.