Wp_Query meta_query不起作用

时间:2017-03-27 14:15:20

标签: wordpress

我试图在meta_query中做相同的事情,但到目前为止还没有成功。

WHERE tm_url_1="" AND tm_url_2="" AND (tm_url_3="" OR tm_embed_code=!="") AND (tm_url_3!="" OR tm_embed_code=="") 

这里是WP_Query。一旦我添加OR子句,就不会返回任何内容。

$args = array(
                'post_status'         => $post_status,
                'posts_per_page'      => $posts_count,
                'ignore_sticky_posts' => 1,
                'post_type'           => $post_type,
                'order_by'            => $order_by,
                'order'               => $order,
                "meta_query"      => array(
                    array('relation' => 'AND',
                        array(
                            'key'     => 'tm_url_1',
                            'value'   => '',
                            'compare' => '='
                        ),
                        array(
                            'key'     => 'tm_url_2',
                            'value'   => '',
                            'compare' => '='
                        ),
                        array(
                            'relation' => 'OR',
                            array(
                                'key'     => 'tm_url_3',
                                'value'   => '',
                                'compare' => '='
                            ),
                            array(
                                'key'     => 'tm_embed_code',
                                'value'   => '',
                                'compare' => '!='
                            )
                        ),
                        array(
                            'relation' => 'OR',
                            array(
                                'key'     => 'tm_url_3',
                                'value'   => '',
                                'compare' => '!='
                            ),
                            array(
                                'key'     => 'tm_embed_code',
                                'value'   => '',
                                'compare' => '='
                            )
                        )
                    )
                    )
            );

没有返回任何内容。我在meta_query中哪里出错?

2 个答案:

答案 0 :(得分:1)

你应该删除media_query参数中的第一个“数组”:

  "meta_query"      => 
                array(
                    'relation' => 'AND',
                    array(
                        'key'     => 'tm_url_1',
                        'value'   => '',
                        'compare' => '='
                    ),
                    array(
                        'key'     => 'tm_url_2',
                        'value'   => '',
                        'compare' => '='
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key'     => 'tm_url_3',
                            'value'   => '',
                            'compare' => '='
                        ),
                        array(
                            'key'     => 'tm_embed_code',
                            'value'   => '',
                            'compare' => '!='
                        )
                    ),
                    array(
                        'relation' => 'OR',
                        array(
                            'key'     => 'tm_url_3',
                            'value'   => '',
                            'compare' => '!='
                        ),
                        array(
                            'key'     => 'tm_embed_code',
                            'value'   => '',
                            'compare' => '='
                        )
                    )
                )

请参阅:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

答案 1 :(得分:1)

以下是为所需条件添加meta_query的正确方法。

$args = array(
    'post_status' => $post_status,
    'posts_per_page' => $posts_count,
    'ignore_sticky_posts' => 1,
    'post_type' => $post_type,
    'order_by' => $order_by,
    'order' => $order,
    'meta_query' => array(
        array(
            'key' => 'tm_url_1',
            'value' => '',
        ) ,
        array(
            'key' => 'tm_url_2',
            'value' => '',
        ) ,
        array(
            'relation' => 'OR',
            array(
                'key' => 'tm_url_3',
                'value' => '',
            ) ,
            array(
                'key' => 'tm_embed_code',
                'value' => '',
                'compare' => '!='
            )
        ) ,
        array(
            'relation' => 'OR',
            array(
                'key' => 'tm_url_3',
                'value' => '',
                'compare' => '!='
            ) ,
            array(
                'key' => 'tm_embed_code',
                'value' => '',
            )
        )
    )
);

请注意,我已经删除了某些数组中compare的默认值,假设它是=,就像你的情况一样。可以看到here的参考资料。