WordPress / Acf Query Posts by a custom field and order them by another

Here’s my problem , I have a custom post type “Emissions” , and i wrote a query to get all posts where the day field is equal to today , and where the field hour is inferior to current time , so that the query is for displaying the “Upcoming” ,

up to that , i’m good , but when it comes to ordering the posts by the field hour , in an ascending order , the output isn’t ordered at all , is there any problem with my code ?

Read More
$args = array(
'posts_per_page' =>3,
'post_type'     => 'emissions',
'meta_query'    => array(
    'relation'      => 'AND',
    array(
        'key'       => 'jour',

        'value'     => $lyoum,
        'compare'   => '='
    ),
    array(
        'key'       => 'horaire',
        'value'     => $douka,
        'compare'   => '<',
        'order_by'      => 'meta_value_num',
        'order' => 'DESC',
    )
));

if anyone wants to help , that would be great ,

ps : variables are

$lyoum = $today , datetime getting the current day,

$douka = $now ,datetime getting the current time,

Thanks in advance.

Related posts

Leave a Reply

1 comment

  1. Your order parameters are misplaced in the WP_Meta_Query args. They instead belong in the main WP_Query args. You also need to specify the meta key to order by.

    . . . 
    'post_type'     => 'emissions',
    'meta_key'      => 'horaire' // specify which key to order by.
    'orderby'       => 'meta_value_num', // move from WP_Meta_Query.
    'order'         => 'DESC', // same as above.
    'meta_query'    => array(
    . . .