Support

Account

Home Forums Front-end Issues Sort post query by date field

Helping

Sort post query by date field

  • I need to show a list of custom-posts sorted by a custom-field “deadline”. The custom field “deadline” is returning the date with the format “d.m.Y”.

    I actually use the following code but the order of the posts is wrong and has no pattern i can decipher. How can i create a query where i can sort the output by a custom field of the type “date”?

    My arguments:

    $args = array(
        'post_type'   => 'club_tasks',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'meta_key' => 'deadline',
        'orderby' => 'meta_value',
        'order' => 'DESC'
    );          

    Thank you so much for your help!

  • I use this i a theme:

    $date_now = date('Y-m-d H:i:s');
    // WP_Query arguments
    $args = array(
        'posts_per_page'	=> 3,
        'post_type'			=> 'post',
        'meta_query' 		=> array(
        array(
            'key'			=> 'end_date',
            'compare'		=> '>=',
            'value'			=> $date_now,
            'type'			=> 'DATETIME'
        )
        ),
        'order'				=> 'ASC',
        'orderby'			=> 'date',
        'meta_key'			=> 'end_date',
        'meta_type'			=> 'DATE'
    );
    
    // The Query
    $query = new WP_Query( $args );
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.