0

how to compare date meta value in Date query? trying to compare meta value between today and next 7 days, tried this in value field %today%, %str_to_time|+ 1 week% but its not working.

2 Answers 2

0

Direct SQL functions like str_to_time won't work within the value field of metaqueries. You need to provide actual date values. To compare between today and the next 7 days, you will need to calculate those dates and then use them in your query.

print your query in jet filter

// Get today's date
$today = date('Y-m-d');

// Calculate the date for 7 days from now
$next_week = date('Y-m-d', strtotime('+7 days'));

// Meta query to compare between today and next 7 days
$date_query = array(
    'key'     => 'your_date_meta_key',
    'value'   => array( $today, $next_week ),
    'compare' => 'BETWEEN',
    'type'    => 'DATE',
);

// Your query arguments
$args = array(
    'post_type' => 'your_post_type',
    'meta_query' => array(
        $date_query,
    ),
);

// Run your WP_Query with these arguments
$query = new WP_Query( $args );
1
  • you said "print your query in jet filter" doesjet filter has that option?
    – vikram
    Commented Apr 6 at 6:50
0

for those who are looking for this,

after going thru various posts and articles, got it right, attached the image of correct way to do it. thanks all who tried to help.

right way to do it. make sure you use "Timestamp" as Data type. use this for example today +3 days today +7 days today +1 weeks today +1 months enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.