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’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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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' ),
);


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x