I need to use the get_posts() function together with a filter, filtering an ACF field
I thought I had it when I read about
meta_query where I could pass in ACF fields and I have this, and I expected it to return just posts that have an event_date bigger/equal a month earlier from today…
$startDate = date('Y-m-d', strtotime('-30 days')); $posts = get_posts([ 'post_type' => 'events', 'post_status' => 'publish', 'meta_query', array( array( 'key' => 'event_date', 'value' => $startDate, 'compare' => '>=', 'type' => 'DATE' )), 'numberposts' => 50, ]); print("<pre>".print_r($posts,true)."</pre>");
But when I log the $posts array I always get every event!
What am I doing wrong?
event_date looks like this in the REST API:
event_date: "2019-10-28 18:00:00"
I would be really glad if somebody could help me out with this.
I also don’t get any errors…
Thanks in advance
Try adding this:
'order' => 'DESC', 'orderby' => 'meta_value',
Or if you don’t plan on adding more meta_query params, this format may work too:
$replaceme = get_posts([ 'posts_per_page' => -1, 'post_type' => 'custom_post_type', 'meta_key' => 'custom_field_name', 'meta_compare' => '>=', // optional 'meta_value' => 'custom_field_value', ]);
I also don’t know the exact syntax but just play with it until it works. 🙂
Thank you for your input.
Well I tried your first idea, and it did not work.
It is just exactly the same.
It is as if the filter is just not applied at all.
Honestly I would have wondered if adding an order would have resolved it.
I then tried your second suggestion, because I indeed don’t need another meta field.
But this also does not work. Exactly the same as above.
Playing with the syntax is difficult if I don’t see any effect at all…
Am i missing something with the syntax? Is there something with Date fields that is different from other filter comparisons? Is there any way to gain more insight what my filter actually does? Like logging an actual comparison, to see which dates are compared and what the outcome of it is?
Thanks in advance.
A date field is not stored as a date in the database. Remove the “type” argument from your meta query. Also alter the date format.
$startDate = date('Ymd', strtotime('-30 days')); $posts = get_posts([ 'post_type' => 'events', 'post_status' => 'publish', 'meta_query', array( array( 'key' => 'event_date', 'value' => $startDate, 'compare' => '>=')), 'numberposts' => 50, ]); print("<pre>".print_r($posts,true)."</pre>");
thanks for your input.
I tried this as well, but unfortunately it does not have any effect either.
When I print out the event_date I get a value like
I think with your code I am comparing my
$startDate which is something like
I assume to use a compare operator of
> I need to have actual numbers.
So using the $startDate works but we would have to compare it against
Could that be the issue?
And how would I transform the
Thanks for your input.Cheers
the value that you see when you use get_field() on a date field is based on the settings of the field, not what is stored in the database. A date field is stored in the format “Ymd”. Are you sure you’re using a date field? Maybe you’re using a date/time field?
Yeah sorry, it is clearly a date/time field.
For a date/time field you need to use the format “Y-m-d H:i:s”
$startDate = date('Y-m-d H:i:s', strtotime('-30 days'));
Unforunately the filtering still does not work:
I have now:
$startDate = date('Y-m-d H:i:s', strtotime('-2 days')); $posts = get_posts([ 'post_type' => 'veranstaltungen', 'post_status' => 'publish', 'order' => 'DESC', 'orderby' => 'meta_value'. 'meta_query', array( array( 'key' => 'event_date', 'value' => $startDate, 'compare' => '>=')), 'numberposts' => -1, ]);
But I would still get all my events displayed. Also clearly the ones that have an event_date smaller than the
What would I have to add to get i to work?
Is it really possible to compare
2019-11-20 19:30:00 >=
I feel like that is not gonna work…
Anything, anyone? I really need this to get working, but I haven’t found any solution…
Try using WP_Query() instead of get_posts().
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
✍️ New Article: Uncommon CSS Properties— Ahmad Shadeed (@shadeed9) July 2, 2020
I wrote about some uncommon useful CSS properties, with their use-cases and browser support. This could be an ongoing article that will be updated from time to time. Happy learning!
🔗 https://t.co/3Bgc40TkMN pic.twitter.com/BfTZhrq57t
© 2020 Advanced Custom Fields. Subscribe