Automattic\WooCommerce\Blocks\BlockTypes\ProductCollection
QueryBuilder::get_date_query
Constructs a date query for product filtering based on a specified time frame.
Method of the class: QueryBuilder{}
No Hooks.
Returns
Array. Date query array; empty if parameters are invalid.
Usage
// private - for code of main (parent) class only $result = $this->get_date_query( $time_frame ): array;
- $time_frame(array) (required)
Associative array with 'operator' (in or not-in) and 'value' (date string).
-
operator(string)
Determines the inclusion or exclusion of the date range. - value(string)
The date around which the range is applied.
-
QueryBuilder::get_date_query() QueryBuilder::get date query code WC 10.3.6
private function get_date_query( array $time_frame ): array {
// Validate time_frame elements.
if ( empty( $time_frame['operator'] ) || empty( $time_frame['value'] ) ) {
return array();
}
// Determine the query operator based on the 'operator' value.
$query_operator = 'in' === $time_frame['operator'] ? 'after' : 'before';
// Construct and return the date query.
return array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
$query_operator => $time_frame['value'],
'inclusive' => true,
),
),
);
}