0

I have a custom post type Event that I use in conjunction with ACF. I have a group called event and inside that group I have a field called date_start. And this is the code that I currently have, but it filters instead of sorts, meaning every past event is not visible.

$args = array( 
    'post_type' => 'events',
    'posts_per_page' => '-1',
    'meta_query' => array(
        array(
            'key' => 'event_date_start' ,
            'compare' => '>=',
            'value' => current_time('Ymd'),
            )
            ),
    'meta_key' => 'event_date_start',
    'orderby' => 'meta_value_num',
    'order' => 'ASC'
);

Also, I have tried this code to no avail from a similar question here.

$args = array( 
    'post_type' => 'events',
    'posts_per_page' => '-1',
    'orderby' => 'meta_value_num ',
    'meta_key' => 'event_date_start',
    'order' => 'ASC'
);

Doing meta_value_num date instead meta_value_num didn't product desired result either. So, my question is, is there a way to sort these events and not filter theme, so every event would be visible and the most current would be displayed first.

Any help or ideas is much appreciated.

Update

adding 'type' => 'DATE', to meta-query didn't solve the problem.

Here is the var_dump($post);

object(WP_Post)[7713]
  public 'ID' => int 974
  public 'post_author' => string '1' (length=1)
  public 'post_date' => string '2020-01-16 13:40:21' (length=19)
  public 'post_date_gmt' => string '2020-01-16 13:40:21' (length=19)
  public 'post_content' => string '' (length=0)
  public 'post_title' => string '24 - 25 февраля' (length=22)
  public 'post_excerpt' => string '' (length=0)
  public 'post_status' => string 'publish' (length=7)
  public 'comment_status' => string 'closed' (length=6)
  public 'ping_status' => string 'closed' (length=6)
  public 'post_password' => string '' (length=0)
  public 'post_name' => string '24-25-fevralya' (length=14)
  public 'to_ping' => string '' (length=0)
  public 'pinged' => string '' (length=0)
  public 'post_modified' => string '2020-01-16 13:40:21' (length=19)
  public 'post_modified_gmt' => string '2020-01-16 13:40:21' (length=19)
  public 'post_content_filtered' => string '' (length=0)
  public 'post_parent' => int 0
  public 'guid' => string 'http://emtway.local/?post_type=events&p=974' (length=48)
  public 'menu_order' => int 5
  public 'post_type' => string 'events' (length=6)
  public 'post_mime_type' => string '' (length=0)
  public 'comment_count' => string '0' (length=1)
  public 'filter' => string 'raw' (length=3)
0

In meta_query, you missed the type of the field:

$args = array( 
'post_type' => 'events',
'posts_per_page' => '-1',
'meta_query' => array(
    array(
        'key' => 'event_date_start' ,
        'compare' => '>=',
        'value' => current_time('Ymd'),
        'type' => 'DATE',
    )
),
'meta_key' => 'event_date_start',
'orderby' => 'meta_value_num',
'order' => 'ASC'

);

3
  • Thanks for your answers, but it's still filtering instead of sorting. Feb 6 '20 at 13:34
  • Could you please var_dump() the result and post here? Feb 6 '20 at 16:40
  • I've added that to my question, but it doesn't contain the ACF fields. Feb 7 '20 at 6:17

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.