Hi. Im trying to use a Meta Query to get posts that are younger than 28 days, but it’s not behaving as it should.
META KEY
post_date
META VALUE
{echo:calculate_28_days_before({current_date:d-m-Y})}
COMPARE
Greater than or equal
TYPE
Date
Now the script i use to calculated the comparison date (28 days before) is this:
<?php
function calculate_28_days_before($date) {
$datetime = DateTime::createFromFormat('d-m-Y', $date);
if ($datetime === false) {
// Handle the error appropriately
return 'Invalid date format';
}
// Subtract 28 days from the date
$datetime->modify('-28 days');
return $datetime->format('d-m-Y');
}
Now the output of both post_date and my function are exactly the same format - d-m-Y.
I know this query should return posts, but it doesn’t. Any good ideas?
Thanks for the tutorial - i don’t love the solution of having to target the ID of the loop in the PHP. Makes it more of a hassle when I have lots of loops to create and maintain. Do you think it’s the only way?
Okay, then thats probably it. Although I also tried to use a custom field using ACF which stored the date instead of post_date, having the same problem. But, your solution works, so thank you - i’ll move on
Unless I’m mistaken, it’s not possible to pass data to a function called with the echo dynamic data tag. I was trying to do this myself for another purpose and ran into an issue as well.
To clarify, you mean in meta query right? Because I can successfully pass data to an echo function outside of meta query just fine - in a code module / text module for instance.