Support

Account

Home Forums Front-end Issues Date orderby is not working

Helping

Date orderby is not working

  • I have a CPT with a few ACF fields(exhibition_status & end_date). I need to only get posts where exhibition_status = ‘past’ and orberby end_date where the most recent dates are first.

    I have written about 15+ versions of a wp_query and the results will not come back as I need. I get close but it still is not properly ordering by date.

    $args = array (
        'post_type'              => 'exhibitions',
        'meta_query'             => array(
            array(
                'key'       => 'end_date',
                'compare'   => '>',
                'value'     => $today,
            ),
        ),
        'meta_key'               => 'exhibition_status',
        'meta_value'             => 'past',
        'orderby'                => 'end_date',
        'order'                  => 'ASC',
        'post_status'            => 'publish',
        'posts_per_page'         => 10,
        'paged'                  => get_query_var( 'paged' ),
    );
  • Well I figured it out but I had to add remove_all_filters('posts_orderby'); before the $args and had to change order to DESC because remove filters reversed the order. Something odd is happening but if someone has an explaination as to why this works please let me know.

    remove_all_filters('posts_orderby');
    
    $args = array (
        'post_type' => 'exhibitions',
        'meta_query'=> array(
            'relation'      => 'AND',
            array(
                'key'       => 'exhibition_status',
                'value'     => 'current',
            ),
            array(
                'key'       => 'end_date',
                'compare'   => 'EXISTS',
            ), 
        ),
        'meta_key'    => 'end_date',
        'orderby'     => 'meta_value',
        'order'       => 'DESC',
        'post_status' => 'publish',
        'posts_per_page' => 10,
        'paged' => get_query_var( 'paged' ),
    );
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Date orderby is not working’ is closed to new replies.