Support

Account

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

Solving

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 );
  • 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.

  • SOLVED

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

    Carry on…

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Sort post query by date field’ is closed to new replies.