meta_query,如何同时使用关系OR和&和? [英] meta_query, how to search using both relation OR & AND?
问题描述
已解决:请参见下面的答案.
Resolved: See answer below.
我有一个名为 BOOKS 的自定义帖子类型.它具有几个自定义字段,分别为:TITLE
,AUTHOR
,GENRE
,RATING
.如何在下面修复我的meta_query
代码,以便仅在自定义字段中包含搜索词的 books :title
,author
,genre
完全一样在搜索表单中指定的rating
会显示在结果中吗?
I have a custom post type called BOOKS. It has several custom fields, named: TITLE
, AUTHOR
, GENRE
, RATING
. How do I fix my meta_query
code below so that only books that have the search word in the custom fields: title
, author
, genre
WITH EXACTLY the rating
specified in my search form, gets displayed in the results?
我已经制作了一个自定义搜索表单;可以搜索title
,author
和genre
的文本区域;和一个将搜索rating
的下拉列表.我在下面进行的meta_query
仅搜索title
,author
和genre
.但是我现在很困惑如何为rating
添加代码.
I have made a custom search form; a text area that will search through the title
, author
and genre
; and a dropdown that will search for the rating
. The meta_query
I made below only searches through the title
, author
, and genre
. But I am now stumped in how to add the code for the rating
.
这是我通过meta_query关系在视觉上想象的方式: (标题或作者或类型)和评级
This is how I visually imagined it with meta_query relation: (title OR author OR genre) AND rating
$args = array(
'relation' => 'OR',
array(
'key' => 'title',
'value' => $searchvalue,
'compare' => 'LIKE'
);
array(
'key' => 'author',
'value' => $searchvalue,
'compare' => 'LIKE'
);
array(
'key' => 'genre',
'value' => $searchvalue,
'compare' => 'LIKE'
);
),
array(
'relation' => 'AND',
array(
'key' => 'rating',
'value' => $ratingvalue,
'compare' => '=',
'type' => 'NUMERIC'
));
非常感谢您的帮助和建议.
I would extremely appreciate your help and advice.
推荐答案
我在一些帮助下找到了解决方案. 下面的代码运行完美.
I found the solution with some help. The code below worked perfectly.
$args => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'title',
'value' => $searchvalue,
'compare' => 'LIKE'
),
array(
'key' => 'author',
'value' => $searchvalue,
'compare' => 'LIKE'
),
array(
'key' => 'genre',
'value' => $searchvalue,
'compare' => 'LIKE'
)
),
array(
'key' => 'rating',
'value' => $ratingvalue,
'compare' => '=',
'type' => 'NUMERIC'
)
)
);
这篇关于meta_query,如何同时使用关系OR和&和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!