具有多个关系键的嵌套元查询

时间:2012-12-05 作者:Andrew Odri

我很好奇Wordpress是否能够运行嵌套meta_query, 每个都有不同的关系键?从Wordpress 3.0开始,tax_query 能够执行此功能;我想知道这是否与meta_query.

$results = query_posts( array(
    \'post_type\' => \'event_id\',
    \'meta_query\' => array(
        \'relation\' => \'AND\',
        array(
            \'relation\' => \'OR\',
            array(
                \'key\' => \'primary_user_id\',
                \'value\' => $user_id
            ),
            array(
                \'key\' => \'secondary_user_id\',
                \'value\' => $user_id
            )
        ),
        array(
            \'key\' => \'date\',
            \'value\' => array( $start_date, $end_date ),
            \'type\' => \'DATETIME\',
            \'compare\' => \'BETWEEN\'
        )
    )
) );
参考文献:

3 个回复
最合适的回答,由SO网友:Gustavo 整理而成

这个问题是针对Wordpress 3.0的,但以防有人对Wordpress Codex的最新版本有同样的问题:

从4.1版开始,可以嵌套meta\\u查询子句以构造复杂的查询

https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters因此,该查询应该在当前Wordpress版本上工作。

SO网友:Ramon Fincken

同时,这也是可能的,请参见文档中的示例和说明:

旧链接:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters更新:2021,新链接:https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters

还有另一个例子https://wordpress.org/support/topic/wp_query-with-multiple-meta_query/#post-9410992

\'meta_query\'  => array(
    \'relation\' => \'OR\',
    array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'_price\',
            \'value\'   => 1,
            \'compare\' => \'>=\',
            \'type\' => \'DECIMAL\',
        ),
        array(
            \'key\'     => \'_price\',
            \'value\' => 3000,
            \'compare\' => \'<=\',
            \'type\' => \'DECIMAL\',
        ),
    ),
    array(
        \'relation\' => \'AND\',
        array(
            \'key\'     => \'_price\',
            \'value\'   => 3001,
            \'compare\' => \'>=\',
            \'type\' => \'DECIMAL\',
        ),
        array(
            \'key\'     => \'_price\',
            \'value\' => 6000, //fixed <= to =>
            \'compare\' => \'<=\',
            \'type\' => \'DECIMAL\',
        ),
    )
),

SO网友:vmassuchetto

这似乎是不可能的。如果我错了,请有人纠正我。

这个meta_query 参数将实际转换为WP_Meta_Query 对象,以及relation 验证不会深入wp-includes/meta.php, 在顶层只发生一次:

if ( isset( $meta_query[\'relation\'] ) && strtoupper( $meta_query[\'relation\'] ) == \'OR\' ) {
    $this->relation = \'OR\';
} else {
    $this->relation = \'AND\';
}
一种可能的解决方案是为该查询构建自己的联接。

$query = new WP_Query( array(
    ...
    \'my_meta_query\' => true,
    \'suppress_filters\' => false
) );

add_filter( \'posts_join\', \'my_meta_query_posts_join\', 10, 2 );
function my_meta_query_posts_join( $join, $query ) {

    if ( empty( $query->query_vars[\'my_meta_query\'] ) )
        return $join;

    global $wpdb;

    $new_join = "
        INNER JOIN {$wpdb->postmeta} pm1 ON 1=1
            AND pm1.post_id = {$wpdb->posts}.ID
            AND pm1.meta_key = \'_some_meta_key\'
            AND pm1.meta_value = \'some_value\'
    ";

    return $join . \' \' . $new_join;
}
如果需要进一步的验证和规则,也可以使用posts_where 滤器

结束

相关推荐

Admin Theme customization

我遵循wordpress codex网站上关于通过插件创建管理主题的说明。我激活了插件,但我的样式表没有包含在<head>.. 这是我的代码:add_action( \'admin_init\', \'kd_plugin_admin_init\' ); add_action( \'admin_menu\', \'kd_plugin_admin_menu\' ); function kd_plugin_admin_init() { /* Register