where的查询中使用多个元键

where的查询中使用多个元键

本文介绍了在wordpress中使用posts_where的查询中使用多个元键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我想为一个以上的元键添加查询,如下所示:

Here I want to add query for more then one meta key as here:

$where .= " AND meta_key='order_assign_delivery_date' AND meta_value LIKE '0000%' And meta_key='order_assign_data' AND meta_value=0";

那么现在该怎么做?

上面的查询不起作用,这很明显

above query not working and that's obvious

请指导我如何在posts_where过滤器的同一查询中使用多个meta_key

Please guide me how to use more than one meta_key in same query in posts_where filter

推荐答案

首先,您必须使用custom_posts_join过滤器

First you have to set up two postmeta using custom_posts_join filter

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

And then you can now set the 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');

让我知道这是否对您有用!

Let me know if this works for you!

这篇关于在wordpress中使用posts_where的查询中使用多个元键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:34