添加一个orderby滤镜,使用多个键进行后处理

时间:2013-05-14 作者:Nate

我使用此筛选器按存储为文本的日期字段(旧数据)对查询进行排序

add_filter( \'posts_orderby\', \'my_posts_orderby_date\', 10, 2 );
function my_posts_orderby_date( $orderby, $query ) {
    global $wpdb;
    return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( \'order\' );
}
我需要告诉这个过滤器使用哪个meta\\u键,因为它当前获取了错误的meta键($state\\u键)进行排序。如何调整此过滤器以实现此目的?

i、 e.我需要以某种方式将其添加到过滤器中

$wpdb->postmeta.meta_key = \'tf_events_startdate\'
这是原始参数集,如果有用的话。非常感谢。

$args = array(
            \'post_type\' => \'tf_events\',         
            \'meta_key\' => $state_key,
            \'meta_value\' => $state_meta_value,
            \'meta_query\' => array( $relation, $date_query, $date_query2 ),
            \'posts_per_page\' => $limit, 
            \'tax_query\' => array( \'relation\' => \'AND\', $type_query, $host_query ),              
            \'order\' => \'ASC\'
        );

1 个回复
SO网友:Nate

从这篇帖子中获得了更改我的查询的想法:How to "orderby" the first array in a meta_query that uses multiply keys?

我将状态查询移动到更大的查询数组中,释放了要分配给事件日期的“meta\\u key”值

$args = array(
    \'post_type\' => \'tf_events\',         
    \'meta_query\' => array( $relation, $date_query, $date_query2, $state_query ),
    \'posts_per_page\' => $limit, 
    \'meta_key\' => \'tf_events_startdate\',
    \'tax_query\' => array( \'relation\' => \'AND\', $type_query, $host_query ),              
    \'order\' => \'ASC\'
    );

add_filter( \'posts_orderby\', \'my_posts_orderby_date\', 10, 2 );
    function my_posts_orderby_date( $orderby, $query ) {
        global $wpdb;  
        return " CAST( $wpdb->postmeta.meta_value AS DATE ) " . $query->get( \'order\' );
    }

结束