wp query - ACF Date Based wp_query

I've been trying to order these exhibitions based on the end_date custom field created with Advanced Custom Fields.

I've been trying to order these exhibitions based on the end_date custom field created with Advanced Custom Fields. I'm not able to properly get this working. I need the most recent dates first. I need to also only get posts with the exhibition_status of past. For the life of me I can't get this working and the below code is just the latest non working interation.

$args = array (
    'post_type'         => 'exhibitions',
    'meta_query'        => array(
        'relation'      => 'OR',
            'query_one'     => array(
                'key'       => 'exhibition_status',
                'value'     => 'past',
            ),
            'query_two'     => array(
                'key'       => 'end_date',
                'compare'   => '>=',
            ), 
    ),
    'orderby'                => 'end_date',
    'order'                  => 'ASC',
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' ),
);

The date is based on this format F j, Y

How do I get all posts ordered by the ACF field end_date and only get posts where the ACF field exhibition_status equals past?

I've been trying to order these exhibitions based on the end_date custom field created with Advanced Custom Fields. I'm not able to properly get this working. I need the most recent dates first. I need to also only get posts with the exhibition_status of past. For the life of me I can't get this working and the below code is just the latest non working interation.

$args = array (
    'post_type'         => 'exhibitions',
    'meta_query'        => array(
        'relation'      => 'OR',
            'query_one'     => array(
                'key'       => 'exhibition_status',
                'value'     => 'past',
            ),
            'query_two'     => array(
                'key'       => 'end_date',
                'compare'   => '>=',
            ), 
    ),
    'orderby'                => 'end_date',
    'order'                  => 'ASC',
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' ),
);

The date is based on this format F j, Y

How do I get all posts ordered by the ACF field end_date and only get posts where the ACF field exhibition_status equals past?

Share Improve this question edited Jul 2, 2020 at 17:14 Sabbir Hasan 1,4091 gold badge9 silver badges14 bronze badges asked Jul 2, 2020 at 15:58 smack-a-brosmack-a-bro 1136 bronze badges 4
  • Ordeby can not be a unknown field. You need to add another parameter 'meta_key' => 'end_date' and set the 'order_by' => 'meta_value'. – Shazzad Commented Jul 2, 2020 at 16:37
  • Unfortunetly, that has no effect on the end results. – smack-a-bro Commented Jul 2, 2020 at 16:53
  • Also the 'relation' => 'OR' should be 'relation' => 'AND' – Shazzad Commented Jul 2, 2020 at 16:54
  • Still the same. I don't get it. Its seems to be kind of sorting them but early dates are still showing before more recent dates in some case.... – smack-a-bro Commented Jul 2, 2020 at 17:04
Add a comment  | 

1 Answer 1

Reset to default 1

I've modified your existing code.Can you please try the code bellow:

$args = array (
    'post_type' => 'exhibitions',
    'meta_query'=> array(
        'relation'      => 'AND',
        array(
            'key'       => 'exhibition_status',
            'value'     => 'past',
        ),
        array(
            'key'       => 'end_date',
            'compare'   => 'EXISTS',
            'type'      => 'DATE'
        ), 
    ),
    'meta_key'    => 'end_date',
    'orderby'     => 'meta_value',
    'order'       => 'ASC',
    'post_status' => 'publish',
    'posts_per_page' => 10,
    'paged' => get_query_var( 'paged' ),
);

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

相关推荐

  • wp query - ACF Date Based wp_query

    I've been trying to order these exhibitions based on the end_date custom field created with Advanced Custom Fields.

    22小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信