定制POST类型中的ORDER BY META_KEY不影响查询

时间:2016-09-30 作者:Rafael Soufraz

我试图通过meta\\u键查询自定义帖子排序。但是在运行WP\\u查询之后,order子句中的sql查询没有任何更改。

$args = array(
\'post_type\'   => \'event\',
\'post_status\' => \'publish\',

\'meta_key\' => \'_start_eventtimestamp\',
\'orderby\' => \'meta_value_datetime\',
\'order\' => \'ASC\',

\'meta_query\' => array(
    array(
        \'key\' => \'_start_eventtimestamp\',
        \'value\' => date(\'Ymd\'),
        \'compare\' => \'>=\'
    )
),

\'posts_per_page\' => \'7\',
);

$event_query = new WP_Query( $args );
当我得到生成的查询时,我得到了:

SELECT SQL_CALC_FOUND_ROWS wp_posts.ID 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_postmeta.meta_key = \'_start_eventtimestamp\'
 AND ( ( mt1.meta_key = \'_start_eventtimestamp\' AND mt1.meta_value >= \'20160930\' ) ) )
 AND wp_posts.post_type = \'event\' AND ((wp_posts.post_status = \'publish\'))
 GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order ASC LIMIT 0, 7
订单条款没有变化
我的按meta\\u键排序的脚本中是否有错误?

1 个回复
SO网友:Syed Fakhar Abbas

请看一下WP_QUERY 改进和WP_QUERY Order & Orderby Parameters.


$args = array( 
         \'post_type\'   => \'event\',
         \'post_status\' => \'publish\',
         \'orderby\' => array(
           \'meta_key\' => \'ASC\'
          ), 
         \'meta_query\' => array(
           array(
             \'key\' => \'_start_eventtimestamp\',
             \'value\' => date(\'Ymd\'),
             \'compare\' => \'>=\'
          ),
       ),
    );
希望它对你有用!

相关推荐

如何在wp-Query中比较不同的时间戳以获取事件自定义帖子类型?

我有一个带有自定义元数据库的自定义帖子类型(as seen on wptheming.com) 并且希望进行查询以显示即将发生的事件,例如在侧边栏中,过去的日期被忽略。但我需要一个当前时间的值来和事件开始时间进行比较。现在我从current_time(\'mysql\') 看起来是这样的:2012-02-16 13:15:31元的开始时间如下所示:201108121100如何使这两个日期具有可比性?我可以在查询中执行吗?或者是否有其他解决方案?以下是我到目前为止的查询(值留空,时间戳回显):<?ph