简体   繁体   中英

Order by custom field value not working for acf date field

I have custom post type event which has custom field event_date. The date format for this field is set to Ymd.

I'm trying to get posts in order of event_date

Here is the code to pull the posts in that order

$today = date('Ymd');

$args = array (
    'post_type'             => array( 'event' ),
    'posts_per_page'        => '-1',
    'order'                 => 'ASC',
    'orderby'              =>'meta_value', // also tried having meta_value_num but that didn't worked as well
    'meta_key'              =>'event_date',
    'meta_query' => array(
        array(
            'key'       => 'event_date',
            'compare'   => '>=',
            'value'     => $today,
        )
    ),
    'suppress_filters' => true
);
/*
global $wp_filter;
var_dump( $wp_filter['posts_orderby'] );die;
This returns NULL as well
*/


    $query = new WP_Query( $args );
    echo '<pre>';
    var_dump($query->request);die;

But the $query->request shows the ORDER BY as post_date rahter than my supplied query. Here is the dump of request

string(681) "SELECT   wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND ( 
  wp_posts.ID NOT IN (
                SELECT object_id
                FROM wp_term_relationships
                WHERE term_taxonomy_id IN (11)
            )
) AND ( 
  wp_postmeta.meta_key = 'event_date' 
  AND 
  ( 
    ( mt1.meta_key = 'event_date' AND mt1.meta_value >= '20171113' )
  )
) AND wp_posts.post_type = 'event' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'acf-disabled' OR wp_posts.post_status = 'private') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC "

can't order by the event_date. What am i doing wrong?? Even tried by having

remove_all_filters('posts_orderby');

before the wp_query still same result.

FINALLY: Thanks everyone for your kind responses, finnally solved by just changing the order_by to orderby in my query. Thanks @Prasanna Venkatesh for letting me check in phpmyadmin. Ran the code in phpmyadmin and fixed it. PS: the sql query was correct except the order_by.

You're missing 'type' => 'DATE' to meta query.

$args = array (
    'post_type'             => array( 'event' ),
    'posts_per_page'        => '-1',
    'order'                 => 'ASC',
    'orderby'              =>'meta_value', // also tried having meta_value_num but that didn't worked as well
    'meta_key'              =>'event_date',
    'meta_query' => array(
        array(
            'key'       => 'event_date',
            'compare'   => '>=',
            'value'     => $today,
            'type'      => 'DATE'
        )
    ),
    'suppress_filters' => true
);

use 'meta_value_num' in your orderby type and

$args = array (
    'post_type'             => array( 'event' ),
    'posts_per_page'        => -1,
    'meta_key'              => 'event_date',
    'orderby'               => 'meta_value_num',
    'order'                 => 'ASC',
    'meta_query' => array(
        array(
            'key'       => 'event_date',
            'compare'   => '>=',
            'value'     => $today,
            'type'      => 'NUMERIC'
        )
    ),
    'suppress_filters' => true
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM