如何在 wp_query 中使用 find_in_set [英] How to use find_in_set in wp_query

查看:31
本文介绍了如何在 wp_query 中使用 find_in_set的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个元键 key1 和 key2,我正在寻找使用这两个键的查询这是我的 php 查询

I have 2 meta key key1 and key2, i am looking for a query for using both key here is my php query

$where = "keyone = 1 or FIND_IN_SET($id,  `keytwo` ) <>0";  

它在 mysql 上运行良好,但是我试图将这个概念转换为 wp_query,但我没有得到任何结果.

its worked fine with the mysql however i am trying to convert this concept into wp_query but i am not getting any results.

某人的参考指南

此代码也不起作用

提前致谢.

推荐答案

我发布的链接只是让您注入自己的自定义 WHERE 子句的一种方式,这可能是您唯一的选择.如果您查看 WP_Meta_Query 的源代码,您将看到比较运算符是硬编码的.因为 WordPress 从来没有真正期望存储 SET 值,所以实际上也没有对这些值的任何本机支持.我会尝试这样的事情:

The link I posted was just a way for you to inject your own custom WHERE clause which is probably your only option. If you look at the source for WP_Meta_Query you'll see that the comparison operators are hard-coded. Because WordPress never really expected to store SET values there really isn't any native support for these, either. I would try something like this:

//Function to inject our custom WHERE clauses
function so_34594266_post_where( $where, &$wp_query )
{
    //Somehow this function needs to be made "aware" of the $id variable,
    //either a class-level variable, global (yuck) or some function call
    if( $wp_query->is_main_query() )
    {
        $where = "keyone = 1 or FIND_IN_SET($id,  `keytwo` ) <>0";
    }

    return $where;
}

//Tell WordPress to use our function
add_filter( 'post_where', 'so_34594266_post_where' );

//Get our posts (this could also be a WP_Query object or whatever else)
$posts = get_posts( '...your stuff here...' );

//Tell WordPress to stop using our function
remove_filter( 'post_where', 'so_34594266_post_where' );

这篇关于如何在 wp_query 中使用 find_in_set的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆