I like to set meta_query for 10 days after date stored in custom field.
I set like this, but it does not work.
Meta key ‘2a’ has value like ‘2022-02-15’.
<?php
$today = wp_date('Y-m-d');
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key'=> '2a',
'value' => array( date("Y-m-d", strtotime("+10 day", strtotime(get_post_meta($post->ID , '2a' ,true)))), $today ),
'compare' => '<=',
'type' => 'DATE',
),
array(
'key'=> '3a',
'compare' => 'EXISTS'
),
));?>
2
Answers
Works now
You’re passing two values into the date portion of your query which suggests you need to compare between as opposed to
<=
.I tend to find it’s better to perform calculations first and then pass the result into the query. Merging it all together as in your example makes it much harder to follow.
While
strtotime()
will work, there are classes that make handling time easier and more readable.Example: