元查询和奇怪的orderby行为

时间:2011-10-28 作者:Be Gneno

这是我的问题查询,它选择所有未来或挂起的事件
请注意,有些帖子只有\\u开始日期,没有\\u结束日期(1天事件)。

$args = array( 
\'post_type\'=> \'events\', 
\'meta_query\' => array(
    array(
    \'key\' => \'_startdate\',
    \'value\' => $today,
    \'type\' => \'DATE\',
    \'compare\' => \'>=\'
    ),
    array(
    \'key\' => \'_enddate\',
    \'type\' => \'DATE\',
    \'value\' => $today,
    \'compare\' => \'>=\'
    ),
    \'relation\' => \'OR\',
    ),
\'meta_key\'=> \'_city\', 
\'orderby\' => \'meta_value\', 
\'order\' => \'ASC\',
) ;
在我改变之前一切都很好_city_startdate_enddate.

您能帮我查询一下(按开始日期排序)吗?谢谢

2 个回复
SO网友:WraithKenny

You could try

$args = array(
    \'post_type\'=> \'events\',
    \'meta_query\' => array(
        array(
            \'key\' => \'_startdate\',
            \'value\' => $today,
            \'compare\' => \'>=\',
            \'type\' => \'DATE\',
        ),
        array(
            \'key\' => \'_enddate\',
            \'value\' => $today,
            \'compare\' => \'>=\',
            \'type\' => \'DATE\',
        ),
        \'relation\' => \'OR\',
    ),
);
function jumpin_thru_hoops( $a ) {
    global $wpdb;
    $a = $wpdb->postmeta.\'.meta_value+0 ASC\';
    return $a;
}
add_filter( \'posts_orderby\', \'jumpin_thru_hoops\' );
$upcoming = new WP_Query( $args );
// or
$upcoming = get_posts( $args );
remove_filter( \'posts_orderby\', \'jumpin_thru_hoops\' );
SO网友:scribu

Replace \'meta_key\'=> \'_city\' with \'meta_key\'=> \'_startdate\'.

结束

相关推荐