admin管理员组

文章数量:1391776

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 '/?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)

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)
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Feb 6, 2020 at 6:30 ivan marchenkoivan marchenko 1491 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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'

);

本文标签: Sort custom post type by custom datepicker instead of filtering