在这里,我想为多个元键添加查询,如下所示:
$where .= " AND meta_key='order_assign_delivery_date' AND meta_value LIKE '0000%' And meta_key='order_assign_data' AND meta_value=0";
那么现在该怎么做呢?
上面的查询不起作用,这很明显
请指导我如何在 posts_where 过滤器的同一查询中使用多个元键
custom_posts_join
首先,您必须使用过滤器设置两个 postmeta
function custom_posts_join($join){
global $wpdb;
$join .= " LEFT JOIN $wpdb->postmeta as meta_1 ON $wpdb->posts.ID = meta_1.post_id LEFT JOIN $wpdb->postmeta as meta_2 ON $wpdb->posts.ID = meta_2.post_id";
return $join;
}
add_filter( 'posts_join' , 'custom_posts_join');
然后你现在可以设置 posts_where
function filter_where( $where = '' ) {
$where .= " AND meta_1.meta_key='order_assign_delivery_date' AND meta_1.meta_value LIKE '0000%' And meta_2.meta_key='order_assign_data' AND meta_2.meta_value=0";
}
add_filter('posts_where', 'filter_where');
让我知道这是否适合您!