dongshui9690
2013-10-11 06:59
浏览 27
已采纳

在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";

so now how to do this?

above query not working and that's obvious

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

图片转代码服务由CSDN问答提供 功能建议

这里我想为多个元键添加查询,如下所示: < pre> $ where。=“AND meta_key ='order_assign_delivery_date'AND meta_value LIKE'0000%'and meta_key ='order_assign_data'AND meta_value = 0”; < 那么现在怎么做呢?

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duankang8114 2013-10-11 07:25
    已采纳

    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');
    

    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!

    点赞 评论

相关推荐 更多相似问题