meta_query with multiple key

时间:2017-11-29 作者:user132319

我有一个搜索输入,然后我想显示在多个meta_key. 我怎样才能做到?

这是我的code

$filter = array(
    \'post_type\'     => \'request\',
    \'post_status\'   => \'publish\',
    \'posts_per_page\' => -1,
    \'meta_query\' => array(
        "relation" => "AND",
        array(
            \'key\' => array($key1, $key2, $key3),
            \'value\' => $search_value,
            \'compare\' => \'LIKE\',
        )
    )
);

1 个回复
SO网友:Jacob Peattie

key 在元查询需要是字符串的情况下,不能向单个元查询传递多个键。您需要为每个键添加查询:

$filter = array(
    \'post_type\'      => \'request\',
    \'post_status\'    => \'publish\',
    \'posts_per_page\' => -1,
    \'meta_query\'     => array(
        \'relation\' => \'OR\',
        array(
            \'key\'     => $key1,
            \'value\'   => $search_value,
            \'compare\' => \'LIKE\',
        ),
        array(
            \'key\'     => $key2,
            \'value\'   => $search_value,
            \'compare\' => \'LIKE\',
        ),
        array(
            \'key\'     => $key3,
            \'value\'   => $search_value,
            \'compare\' => \'LIKE\',
        ),
    ),
);
注意,我设置\'relation\'\'OR\' 因此,将为匹配任何键(而不是所有键)的帖子返回结果。如果需要结果来匹配所有键,请将其更改回\'AND\'.

结束

相关推荐