I was using a custom post let's say 'question' where user publishes the 'question' with a 'post meta' called 'course', in which multiple 'course names' stored as course id's(as a string separated with comma). I am writing a search functionality where user enters course name then by using that 'course name' I get the 'course id', My Question here is that how to search for my 'course id' with 'meta_query' called 'course' stored as a string? I am using WP_Query for this, below is my code: While Storing:

  $course_registered = '2013,1919,1827'; // Saving Multiple Course names as Course ID's
  $post = array(
        'post_title'    => $title,
        'post_content'  => $description,
        'post_status'   => 'draft', 
        'post_type' => "question"  // Use a custom post type if you want to
  update_post_meta( $this->post_ID, 'inistitute_name', $institute_name );
  update_post_meta( $this->post_ID, 'course', $course_registered );

While searching query:

       global $wpdb;
       $course_key = $_GET['tewa_search'];

       $query= "SELECT ID FROM `wp_posts` WHERE wp_posts.post_title LIKE '$course_key%' and wp_posts.post_type='courses'";
       $wp_query = $wpdb->get_results($query, ARRAY_N);
       if( ! empty($wp_query) ){    
          $keyword = $wp_query[0][0];   
          $keyword = $_GET['tewa_search'];  

       $keyword = $_GET['search_keyword'];
       $institute_name = get_user_meta($user_ID, 'inistitute_name', true);
       $status=array('publish','pending', 'draft');
       $paged = (get_query_var('page')) ? get_query_var('page') : 1;
       $the_query = new WP_Query(array(
         'post_type' => 'question',
         array("s" => $keyword),
         'meta_query' => array (
         'relation' => 'OR',
         array (
           'key' => 'course',
           'value' => $keyword, //$keyword = 1827 (getting course id based on user entered keyword)
           'compare' => 'IN'
        array (
            array (
              'key' => 'inistitute_name',
              'value' => array ($institute_name),
              'compare' => 'IN'
    'posts_per_page' => '5',
    'post_status' => 'publish',
    'paged' =>$paged,
    'post_status' => $status,
    'order' => 'DESC'

Seriously It is driving me nuts, Please help me. Can anyone please tell me what's wrong in this? Thanks in advance.

  • remove array("s" => $keyword) as it is search all post which contain $keyword value, what institute value is about? multiple 'course names' stored as course id's(as a numeric array) means? – Govind Kumar Nov 25 '16 at 5:12
  • @GovindKumar, I updated above code plz check it once. I am storing a numeric array values into post_meta of 'course' . – Prasad Patel Nov 25 '16 at 6:04
  • why you are passing $keyword to course meta_value, does this variable contain course id or search query? also don't use get_user_meta to get post meta value of post use get_post_meta instead . your code is totally, there are many errors. i can help you edit question and write your proper requirement . – Govind Kumar Nov 25 '16 at 6:22
  • if user entered keyword is course name then I will get the corresponding course id and use it in 'course' post_meta or else if keyword is something else like 'post_title', 'post_content', etc. I am using WP_Query 's 's'=>$keyword for this. – Prasad Patel Nov 25 '16 at 6:31
  • An array is a php data structure, that value gets converted to a serialized string to get stored in the database. An IN comparison won't work to search within a string, it's looking for an exact match. – Milo Nov 25 '16 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.