dongyu3967 2017-11-13 03:27
浏览 87

按自定义字段值排序,不适用于acf日期字段

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.

  • 写回答

2条回答 默认 最新

  • dongzhan1383 2017-11-13 04:51
    关注

    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
    );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line