带有子关系的 Wordpress 查询 [英] Wordpress query with sub relations

查看:35
本文介绍了带有子关系的 Wordpress 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让 meta_query 带有子关系?我不希望一个 relation 应用于所有查询的数组.

Is there a way to have a meta_query with sub relations? I don't want one relation to apply to all of the query's arrays.

我试过了,但无济于事:

I tried this, but to no avail:

'meta_query' => array(
    array(
        'key' => 'event_type',
        'value' => 'session',
        'compare' => 'LIKE'
    ),
    array(
        'relation' => 'OR',
        array(
            'key' => 'event_video',
            'value' => '0',
            'compare' => '>'
        ),
        array(
            'key' => 'event_summary',
            'value' => '',
            'compare' => '!='
        )
    ),
    array(
        'key' => 'event_date_single',
        'value' => '',
        'compare' => 'LIKE'
    ),
    array(
        'key' => 'event_start_time',
        'value' => '',
        'compare' => 'LIKE'
    )
)

所以 'relation' =>'OR' 应该只适用于 event_videoevent_summary 之间.其他一切都是相互独立的.这有可能吗?

So 'relation' => 'OR' should only apply between event_video and event_summary. Everything else is independent of one another. Is this possible somehow?

谢谢

推荐答案

看起来这可能是 meta_query 的限制.WP Codex 表明您可以将关系用于元查询,但据我所知,限制是你只能对整个meta_query使用一种关系.

It looks like this could be a meta_query limitation. WP Codex shows that you can use relations for meta queries, but as far as I see the limitation is that you can only use one kind of relation for the whole meta_query.

这意味着您可以在 meta_query 参数之间拥有完整的 OR 或完整的 AND(默认)关系,但您永远无法拥有它们两者结合,这正是你想要的.

This means you can have a full OR or a full AND (default) relation between meta_query parameters, but you can never have them both combined, which is just what you want.

因为我在 WP Codex 遇到它时并不高兴,所以我通过手动搜索代码来更深入地了解这个问题.所以我发现WP_Query 调用WP_Meta_Query 来解析meta_query 参数.这是 WP_Meta_Query 的类构造函数(您可以在 WP 3.6 的 wp-includes/meta.php643 中找到它):

As I was not glad when I faced it at WP Codex, I went deeper in the matter by manually searching the code. So I found out that WP_Query calls WP_Meta_Query to parse meta_query parameters. This is WP_Meta_Query's class constructor (you can find it in wp-includes/meta.php at line 643 in WP 3.6):

/**
 * Constructor
 *
 * @param array $meta_query (optional) A meta query
 */
function __construct( $meta_query = false ) {
    if ( !$meta_query )
        return;

    if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
        $this->relation = 'OR';
    } else {
        $this->relation = 'AND';
    }

    $this->queries = array();

    foreach ( $meta_query as $key => $query ) {
        if ( ! is_array( $query ) )
            continue;

        $this->queries[] = $query;
    }
}

长话短说,第二个 if 表明这个类只能与 ORAND 关系一起工作,但是 从不两者结合.

Long story short, the second if shows that this class will work only either with a OR or AND relation, but never with both of them combined.

因此,不幸的是,似乎无法实现您想要做的事情(至少在 WP 3.6 之前).我想你最好的办法是拆分主查询,首先做特定的元查询,然后应用它们结果(返回 metas)到您的主查询.

So, unfortunately, it seems that it's not possible to achieve what you are trying to do (at least until WP 3.6). I guess your best shot is to split the main query, first doing specific Meta Queries and then applying their results(returned metas) to your main query.

我知道这不是解决方案而是答案.尽管如此,我希望这可以帮助您找到解决此问题的方法.

I know that this is not a solution but an answer. Nevertheless, I hope this may help you to find a solution for this matter.

这篇关于带有子关系的 Wordpress 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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