I am using Wordpress with Advanced Custom Fields. I have a custom post called "event" with a custom field "date" (I have added the field through ACF). The date field is formatted in the following way: "yyyy-mm-dd." Until now, sorting the custom posts worked fine using the following snippet:
$query = new WP_Query([
'post_type' => 'event',
'meta_key' => 'tq_event_date',
'meta_value' => date('Y-m-d'),
'meta_compare' => '<',
'orderby' => ['meta_key' => 'tq_event_date'],
'posts_per_page' => '-1'
]);
if($query->have_posts()) {
$posts = $query->get_posts();
foreach($posts as $post) {
echo get_field('tq_event_date', $post->ID). ' ';
}
}
Now I have added a new event post with date "2015-12-03." The sorting for some reason does not sort this post. Here are the results of this query:
2015-08-15
2015-07-15
2015-06-02
2015-05-18
2015-04-21
2015-04-18
2015-04-17
2015-04-02
2015-03-28
2014-11-25
2014-07-14
2014-07-04
2014-06-08
2014-05-31
2014-04-25
2014-04-26
2014-03-14
2014-03-15
2014-02-23
2014-02-23
2013-12-16
2013-11-19
2013-11-12
2013-07-24
2013-06-09
2013-05-12
2013-04-26
2013-04-19
2013-04-10
2013-04-05
2012-12-08
2012-11-02
2012-10-22
2012-10-09
2012-10-07
2012-06-23
2012-03-07
2012-02-12
2012-02-07
2011-06-18
2011-05-26
2011-04-30
2011-04-06
2011-02-26
2011-02-11
2011-01-28
2010-12-28
2010-11-22
2010-10-30
2010-10-16
2010-09-16
2010-05-29
2010-05-05
2010-03-29
2010-03-23
2015-12-03
For some reason, the last post is the one that I just added. I really cannot find the reason for this problem's existence... What am I doing wrong?
EDIT: Changed the custom field from date
to tq_event_date
but still didn't work.
date
is your custom field right? I pretty sure thatdate
is already "taken" and is used as one of WP_Query's default fields. (ref: WP_Query) Change your meta_key in database to somethink likeevent_date
or even better:{your or your company creds}_event_date
likers_event_date
. This way it never conflicts with default WP phrases.meta_key
's are updated and everything is correct in database 3. Safest date format for code isyyyymmdd
without any dots and dashes. You can format it to something beautiful before showing it in front-endYmd
,Y-m-d
orY/m/d
. What does matter is that you cannot have a mix. so you cannot have some dates asYmd
and others asY-m-d