On this page
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.
Parameters
$date_queryarray Required-
Array of date query clauses.
...$0arraycolumnstringOptional. 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'.comparestringOptional. The comparison operator. Accepts'=','!=','>','>=','<','<=','IN', 'NOT IN','BETWEEN', 'NOT BETWEEN'. Default'='.relationstringOptional. The boolean relationship between the date queries. Accepts'OR'or'AND'.
Default'OR'....$0arrayOptional. An array of first-order clause parameters, or another fully-formed date query.beforestring|arrayOptional. Date to retrieve posts before. Acceptsstrtotime()-compatible string, or array of 'year', 'month', 'day' values.yearstringThe four-digit year. Default empty. Accepts any four-digit year.monthstringOptional when passing array.The month of the year.
Default (string:empty)|(array:1). Accepts numbers 1-12.daystringOptional when passing array.The day of the month.
Default (string:empty)|(array:1). Accepts numbers 1-31.
afterstring|arrayOptional. Date to retrieve posts after. Acceptsstrtotime()-compatible string, or array of 'year', 'month', 'day' values.yearstringThe four-digit year. Accepts any four-digit year. Default empty.monthstringOptional when passing array. The month of the year. Accepts numbers 1-12.
Default (string:empty)|(array:12).daystringOptional when passing array.The day of the month. Accepts numbers 1-31.
Default (string:empty)|(array:last day of month).
columnstringOptional. 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. Default is the value of top-level$column.comparestringOptional. 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'='.inclusiveboolOptional. Include results from dates specified in'before'or'after'. Default false.yearint|int[]Optional. The four-digit year number. Accepts any four-digit year or an array of years if$comparesupports it. Default empty.monthint|int[]Optional. The two-digit month number. Accepts numbers 1-12 or an array of valid numbers if$comparesupports it. Default empty.weekint|int[]Optional. The week number of the year. Accepts numbers 0-53 or an array of valid numbers if$comparesupports it. Default empty.dayofyearint|int[]Optional. The day number of the year. Accepts numbers 1-366 or an array of valid numbers if$comparesupports it.dayint|int[]Optional. The day of the month. Accepts numbers 1-31 or an array of valid numbers if$comparesupports it. Default empty.dayofweekint|int[]Optional. The day number of the week. Accepts numbers 1-7 (1 is Sunday) or an array of valid numbers if$comparesupports it.
Default empty.dayofweek_isoint|int[]Optional. The day number of the week (ISO). Accepts numbers 1-7 (1 is Monday) or an array of valid numbers if$comparesupports it.
Default empty.hourint|int[]Optional. The hour of the day. Accepts numbers 0-23 or an array of valid numbers if$comparesupports it. Default empty.minuteint|int[]Optional. The minute of the hour. Accepts numbers 0-59 or an array of valid numbers if$comparesupports it. Default empty.secondint|int[]Optional. The second of the minute. Accepts numbers 0-59 or an array of valid numbers if$comparesupports it. Default empty.
}
$default_columnstring OptionalDefault 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'Source
File:
wp-includes/class-wp-date-query.php. View all referencespublic 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 ); }Related
Uses
Uses Description WP_Date_Query::sanitize_relation() wp-includes/class-wp-date-query.php Sanitizes a ‘relation’ operator.
WP_Date_Query::sanitize_query() wp-includes/class-wp-date-query.php Recursive-friendly query sanitizer.
esc_sql() wp-includes/formatting.php Escapes data for use in a MySQL query.
WP_Date_Query::validate_column() wp-includes/class-wp-date-query.php Validates a column name parameter.
WP_Date_Query::get_compare() wp-includes/class-wp-date-query.php Determines and validates what comparison operator to use.
Used By
Used By Description WP_Site_Query::get_site_ids() wp-includes/class-wp-site-query.php Used internally to get a list of site IDs matching the query vars.
WP_Comment_Query::get_comment_ids() wp-includes/class-wp-comment-query.php Used internally to get a list of comment IDs matching the query vars.
WP_Query::get_posts() wp-includes/class-wp-query.php Retrieves an array of posts based on query variables.
WP_User_Query::prepare_query() wp-includes/class-wp-user-query.php Prepares the query variables.
Changelog
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_date_query/__construct