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


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(
            '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 );
    • Enaldi

    • August 24, 2019 at 4:32 am

    I’m having the same problem. The docs say to use the original poster’s approach, which does not work. Any updates? Setting meta_type does not work for me either.

    • Enaldi

    • August 24, 2019 at 4:53 am


    The “update” is, check my work for freakin typos! /tears

    Carry on…

Viewing 4 posts - 1 through 4 (of 4 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.