Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion includes/Traits/Meta_Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function parse_meta_query( $meta_query_data ) {
$meta_queries[] = array_filter(
array(
'key' => $query['meta_key'] ?? '',
'value' => $query['meta_value'] ?? '',
'value' => $this->map_date_functions( $query['meta_value'] ?? '' ),
'compare' => $query['meta_compare'] ?? '',
'type' => $query['meta_type'] ?? '',
)
Expand All @@ -34,4 +34,33 @@ public function parse_meta_query( $meta_query_data ) {

return array_filter( $meta_queries );
}

/**
* Enable a selection of dynamic date functions for meta queries.
*
* @param string $value Argument to be passed to WP_Meta_Query.
*/
protected function map_date_functions( $value ) {
return str_ireplace(
[
'NOW()',
'HOUR()',
'DAY()',
'MONTH()',
'YEAR()',
'WEEK()',
'UNIX_TIMESTAMP()',
],
[
current_time( 'Y-m-d H:i:s' ),
intval( date( 'H' ) ),
intval( date( 'd' ) ),
intval( date( 'm' ) ),
intval( date( 'Y' ) ),
intval( date( 'W' ) ),
time(),
],
$value
);
}
}
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Curate a list of posts to exclude from the query.

Generate complicated post meta queries using an interface that allows you to create a query based on `meta_key`, `meta_value` and the `compare` options. Combine multiple queries and determine if they combine results (OR) or narrow them down (AND).

You can use MySQL date functions as meta query values for dynamic queries. Supported functions are `NOW()`, `HOUR()`, `DAY()`, `MONTH()`, `YEAR()`, `WEEK()`, and `UNIX_TIMESTAMP()`.

#### Date Query

Query items before/after the current or selected or choose to show the post from the last 1, 3, 6 and 12 months.
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Create powerful meta queries without touching code:
* **Flexible comparisons**: Use equals, not equals, greater than, less than, and more
* **Logical operators**: Combine queries with AND/OR logic
* **ACF integration**: Works seamlessly with Advanced Custom Fields
* **Dynamic date functions**: Use MySQL date functions when casting meta as DATETIME

==== 📅 Dynamic Date Queries ====

Expand Down
27 changes: 27 additions & 0 deletions tests/unit/Meta_Query_Tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,33 @@ public function data_single_meta_query() {
),
),
),
'single query with dynamic date function' => array(
// Custom data.
array(
'meta_query' => array(
'queries' => array(
array(
'meta_key' => 'date',
'meta_value' => 'YEAR()-MONTH()-DAY()',
'meta_compare' => '>=',
'meta_type' => 'DATE',
),
),
),
),
// Expected results.
array(
'is_aql' => true,
'meta_query' => array(
array(
'key' => 'date',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
'type' => 'DATE',
),
),
),
),
'single query without compare and type' => array(
// Custom data.
array(
Expand Down
Loading