admin管理员组文章数量:1431943
I have written a search query using WP_Query
, where am searching for 2 meta_query
values like below:
$keyword = $_GET['tewa_search'];
$institute_name = get_user_meta($user_ID, 'inistitute_name', TRUE);
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = [
'post_type' => 'messages',
'meta_query' => [
'relation' => 'OR',
[
'key' => 'inistitute_name',
'value' => [ $institute_name ],
'compare' => 'IN',
],
[
//course values stored as ID's like 200, 300 in database
'key' => 'course',
//Single course ID if user types 'java' then I can get only single course ID
'value' => $keyword,
'compare' => 'LIKE',
],
],
'posts_per_page' => -1,
'paged' => $paged,
'post_status' => $status,
];
$the_query = new WP_Query($args);
While storing the meta_key
'course'
if user selects multiple courses then the corresponding course ID's am storing in the database, suppose if user selects 2 courses then am storing 2 course ID's separated with comma(100, 200). While searching, if user enters a course name then I will get the corresponding course ID and then search it with the course Id's stored as course IDs are separated with comma(100, 200, 300).
But my query is not working as expected, can anyone tell me whats wrong in the code?
I have written a search query using WP_Query
, where am searching for 2 meta_query
values like below:
$keyword = $_GET['tewa_search'];
$institute_name = get_user_meta($user_ID, 'inistitute_name', TRUE);
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$args = [
'post_type' => 'messages',
'meta_query' => [
'relation' => 'OR',
[
'key' => 'inistitute_name',
'value' => [ $institute_name ],
'compare' => 'IN',
],
[
//course values stored as ID's like 200, 300 in database
'key' => 'course',
//Single course ID if user types 'java' then I can get only single course ID
'value' => $keyword,
'compare' => 'LIKE',
],
],
'posts_per_page' => -1,
'paged' => $paged,
'post_status' => $status,
];
$the_query = new WP_Query($args);
While storing the meta_key
'course'
if user selects multiple courses then the corresponding course ID's am storing in the database, suppose if user selects 2 courses then am storing 2 course ID's separated with comma(100, 200). While searching, if user enters a course name then I will get the corresponding course ID and then search it with the course Id's stored as course IDs are separated with comma(100, 200, 300).
But my query is not working as expected, can anyone tell me whats wrong in the code?
Share Improve this question edited Apr 14, 2019 at 11:10 norman.lol 3,2413 gold badges30 silver badges35 bronze badges asked Nov 28, 2016 at 11:06 Prasad PatelPrasad Patel 1715 silver badges14 bronze badges 3 |1 Answer
Reset to default 0I finally got a solution by making some changes in my code while saving the post I was storing the courses as a string like: " 100, 200, 300 " and If user enters a keyword if that keyword matches with a course then I am getting that corresponding course ID and then using the WP_Query with the following parameters:
$args = array(
'post_type' => 'messages',
'meta_query' => array (
'relation' => 'AND',
array(
'key' => 'inistitute_name',
'value' => array ($institute_name),
'compare' => 'IN'
),
array(
'key' => 'course', //course values stored as ID's like 200, 300 in database
'value' => $keyword, //Single course ID if user types 'java' then I can get only single course ID
'compare' => 'LIKE'
)
),
'posts_per_page' => -1,
'paged' => $paged,
'post_status' => $status,
);
$the_query = new WP_Query($args);
Previously what was the mistake I did is while saving the post I was storing the multiple course names as numeric array itself that causes MySql engine to store that array Serializable string which is difficult to search. But now I am saving the multiple course names as a string separated by commas like this : 123, 456, 789
本文标签: wp queryWhy does WPQuery not search for two 39metaquery39 keys separated with OR
版权声明:本文标题:wp query - Why does WP_Query not search for two 'meta_query' keys separated with OR? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745591022a2665197.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
$_GET['tewa_search']
is an array (multiple courses) ? If yes, then you you should be usingIN
as thecompare
value of your second array in themeta_query
array – Dan. Commented Nov 28, 2016 at 12:08OR
instead ofAND
in your meta query – Dan. Commented Nov 28, 2016 at 15:21