1

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?

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. 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.... Commented Jul 2, 2020 at 17:04

1 Answer 1

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' ),
);
4
  • Still giving me the same result. Commented Jul 2, 2020 at 17:41
  • Well, I think I figured it out. I had the feeling something else was effecting the order besides end_date. I added this before the $args: remove_all_filters('posts_orderby');. That seemed to give the correct results but it was reverse order so I changed the order to DESC and everthing seems to be in the correct order... Not really sure what the heck is happening but if there is a more proper way to fix this let me know. Commented Jul 2, 2020 at 17:47
  • Thanks for the help Sabbir. You deserve the credit for this one. Commented Jul 2, 2020 at 18:06
  • You are welcome Commented Jul 2, 2020 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.