wp query - Why does WP_Query not search for two 'meta_query' keys separated with OR?

I have written a search query using WP_Query, where am searching for 2 meta_query values like below:$keyword = $_GET[�

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
  • I take it from your description that $_GET['tewa_search'] is an array (multiple courses) ? If yes, then you you should be using IN as the compare value of your second array in the meta_query array – Dan. Commented Nov 28, 2016 at 12:08
  • @Dan., No its not that $_GET['tewa_search'] is not an array of multiple courses. It has only a single course ID, like suppose if user enters a course name 'Java' then I will get the corresponding course ID and compare it with the meta_key 'course' stored as a course string like " 100, 200, 300 " for each post. – Prasad Patel Commented Nov 28, 2016 at 14:45
  • Oh ok. I'm not sure what the issue is, it's difficult to know what you mean by 'it's not working as expected' without knowing more about what you are trying to achieve. Could it be not working as expected because you are doing OR instead of AND in your meta query – Dan. Commented Nov 28, 2016 at 15:21
Add a comment  | 

1 Answer 1

Reset to default 0

I 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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745591023a4634831.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信