0

I try to get previous and next post so I do this :

    $args = array(
        'post_type'      => 'event',
        'post_status'    => 'publish',
        //'posts_per_page' => 1,
        'meta_key'       => '_start_at',
        'meta_type'      => 'DATETIME',
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
    );
    $query = new WP_Query( $args );

But when I look at the list it does not matching with my posts... I have an event in february, next in august, next in march... How can I do ? Thandks

1 Answer 1

0

You should be able to set the orderby value to meta_value_datetime. More info at WordPress: https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

Try this:

$args = array(
        'post_type'      => 'event',
        'post_status'    => 'publish',
        'meta_key'       => '_start_at',
        'meta_type'      => 'DATETIME',
        'orderby'        => 'meta_value_datetime',
        'order'          => 'ASC',
);
$query = new WP_Query( $args );

Maybe there's an issue with the date value formatting. You could also try removing the meta_type:

$args = array(
        'post_type'      => 'event',
        'post_status'    => 'publish',
        'meta_key'       => '_start_at',
        'orderby'        => 'meta_value',
        'order'          => 'ASC',
);
$query = new WP_Query( $args );
1
  • It does not work : it is order by published date not by start_at date... I don't understand why It does not work
    – Gpi
    Jul 12 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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