Sort custom post type by most current date picker

I'm trying to sort custom post type made with ACF plugin by most current date. Ideally future most current post sho

I'm trying to sort custom post type made with ACF plugin by most current date. Ideally future most current post should be first. Currently, I'm sorting by date, which gives me the earliest post as the first post.

Here is my code.

$loop = new WP_Query( array( 
    'post_type'             => 'events', 
    'posts_per_page'        => -1,
    'meta_key'              => get_sub_field( 'date_start' ),
    'orderby'               => 'meta_value_num',
    'order'                 => 'ASC'
) ); 

Here is the screenshot of my website to make things clear.

I have tried this post to no avail

It seams that my meta_query is not properly setup. This is the meta_query that I tried.

'meta_query' => array(
    array(
        'key' => get_sub_field( 'date_start' ),
        'compare' => '>=',
        'value' => current_time('Ymd'),
        )
),

note: for some reason I have to do get_sub_field('date_start') plain 'date_start' doesn't return any results.

I'm trying to sort custom post type made with ACF plugin by most current date. Ideally future most current post should be first. Currently, I'm sorting by date, which gives me the earliest post as the first post.

Here is my code.

$loop = new WP_Query( array( 
    'post_type'             => 'events', 
    'posts_per_page'        => -1,
    'meta_key'              => get_sub_field( 'date_start' ),
    'orderby'               => 'meta_value_num',
    'order'                 => 'ASC'
) ); 

Here is the screenshot of my website to make things clear.

I have tried this post to no avail https://stackoverflow/questions/28951370/sort-wp-query-by-acf-datepicker

It seams that my meta_query is not properly setup. This is the meta_query that I tried.

'meta_query' => array(
    array(
        'key' => get_sub_field( 'date_start' ),
        'compare' => '>=',
        'value' => current_time('Ymd'),
        )
),

note: for some reason I have to do get_sub_field('date_start') plain 'date_start' doesn't return any results.

Share Improve this question asked Jan 23, 2020 at 6:55 ivan marchenkoivan marchenko 1491 silver badge6 bronze badges 5
  • Is date_start a sub-field of a repeater or group field? Also, get_sub_field() and get_field() return the value for the current post. It doesn't make any sense to use it as the key. – Jacob Peattie Commented Jan 23, 2020 at 7:01
  • date_start belongs to a group. This totally make sense, but for some odd reason I can't get any result without using get_sub_field(). – ivan marchenko Commented Jan 23, 2020 at 7:30
  • 1 If you're getting a result with get_sub_field(), it's almost certainly incorrect, and by coincidence. If date_start is the sub field of a group, then the meta key will not be date_start, it will be prefixed with the group's key. – Jacob Peattie Commented Jan 23, 2020 at 7:36
  • Thanks, that actually make perfect sense. – ivan marchenko Commented Jan 23, 2020 at 8:30
  • @Jacob, thanks for your input, it did the trick. Learn something everyday – ivan marchenko Commented Jan 23, 2020 at 8:39
Add a comment  | 

1 Answer 1

Reset to default 1

So thanks to Jacob Peattie and his helpful comments I was able to resolve this issue.

meta_key was part of the group, but it wasnt prefixed by a group key. So, something like this totally works for me: group_field_sub_field. So changing that in both arrays did the trick.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744810874a4595050.html

相关推荐

  • Sort custom post type by most current date picker

    I'm trying to sort custom post type made with ACF plugin by most current date. Ideally future most current post sho

    2天前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信