Support

Account

Home Forums Reply To:

  • Hi,

    I tried to order my custom post-type (called “vehicle”) by a custom date field I created, called “date_sell”. I tried different ways (with “meta_value” and “meta_value_num” as “orderby” value), but none of them has worked. Here’s the simpliest code I tried to find the problem.

    Simple function :

    function eai_get_sold_vehicles_by_year($year){

    $args = array(
    ‘posts_per_page’ => -1,
    ‘post_type’ => ‘vehicle’,
    ‘order’ => ‘DESC’,
    ‘meta_key’ => ‘date_sell’,
    ‘orderby’ => ‘meta_value_num’, // also tried with ‘meta_value’
    ‘meta_query’ => array(
    ‘relation’ => ‘AND’,
    array(
    ‘key’ => ‘date_sell’,
    ‘value’ => array(”),
    ‘compare’ => ‘NOT IN’
    ),
    array(
    ‘key’ => ‘date_sell’,
    ‘value’ => ($year + 1) . ‘0101’,
    ‘compare’ => ‘<‘
    ),
    array(
    ‘key’ => ‘date_sell’,
    ‘value’ => $year . ‘0101’,
    ‘compare’ => ‘>=’
    ),
    array(
    ‘key’ => ‘price_sell’,
    ‘value’ => array(”),
    ‘compare’ => ‘NOT IN’
    )
    )
    );

    $query = new WP_Query($args);

    if($query->have_posts()) : while ($query->have_posts() ) : $query->the_post();
    echo ” . get_field(“date_sell”);
    endwhile;
    endif;

    }

    Return :

    23/12/2018
    24/10/2018
    07/10/2018
    09/12/2018
    12/11/2018

    Here the vehicles are listed as they come in the post-type list, so with the default behaviour, ordered by date of publication.

    I’ve searched on other threads, but I still can’t find what I did wrong here.
    Can someone please help me on this ?

    Thank you in advance,

    N.