0

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 that date 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 like event_date or even better: {your or your company creds}_event_date like rs_event_date. This way it never conflicts with default WP phrases. – N00b Dec 17 '15 at 14:14
  • I changed the event type but the result is still the same. For some reason, the newest post is not being sorted – Gasim Dec 17 '15 at 14:38
  • 1. Update your question with fixes 2. Make sure all meta_key's are updated and everything is correct in database 3. Safest date format for code is yyyymmdd without any dots and dashes. You can format it to something beautiful before showing it in front-end – N00b Dec 17 '15 at 14:47
  • I checked all the meta keys and everything is fine in the database. I even wrote a query to sort all the posts based on the meta keys using SQL; and the result worked. However, wp_query doesn't work. This problem does not make any sense because for some reason the post that is created today is shown at the very end while all other posts with random orders shows it correctly... – Gasim Dec 17 '15 at 14:55
  • @N00b The date format is correct, custom fields are sorted literally so it should not make a difference if all your dates are saved as Ymd, Y-m-d or Y/m/d. What does matter is that you cannot have a mix. so you cannot have some dates as Ymd and others as Y-m-d – Pieter Goosen Dec 17 '15 at 15:13
0

The line:

'orderby' => ['meta_key' => 'tq_event_date']

didn't work. So, based on my context, where all the dates start with a number, I was able to sort the items in the following way:

'orderby' => 'meta_value'

If in the future something changes, I think the best way to solve the problem is doing it in PHP:

usort($posts, function($a, $b) {
    $d1 = get_field('tq_event_date', $a);
    $d2 = get_field('tq_event_date', $b);
    return strcmp($d2, $d1);
});
| improve this answer | |
0

The problem might be that you don't specify the type of meta_query which is alphabetical by default and might mess up numberic queries as stated in WP_Query codex.

Try this:

//Note how:

   //1. meta_query is also array 
   //2. I specify that meta_value is numeric and type is date

$event_args = array(
            'post_type'         => 'event',
            'posts_per_page'    => -1,
            'meta_query'        => array(
                                       array(
                                           'key'            => 'tq_event_date',
                                           'meta_value_num' => date(Y-m-d),
                                           'type'           => 'DATE', //You can also try 'NUMERIC'
                                           'compare'        => '<',
                                       )
                                   ),
            'orderby' => 'meta_value',
);

$events = new WP_Query( $event_args );

//etc
| improve this answer | |
  • This didn't work but I found a solution. – Gasim Dec 17 '15 at 15:08
  • What was the problem? – N00b Dec 17 '15 at 15:10
  • Read my answer below. – Gasim Dec 17 '15 at 18:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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