How can I implement Search feature for Membership site?

I have a membership site where I am using Wishlist membership plugin. There are few Courses for which the user signed up and after signing up, they can get access to different modules and their corresponding lessons. All these lessons have multiple videos, files and pdfs attached to them via custom meta boxes( values are getting stored as serialized array in wp_postmeta table ). Moreover, each user can add his/her notes for each specific lessons( values are getting stored in a custom notes table ).

Now, I have to add Search feature on my site which should search for the following things:

Read More
  1. Post Content
  2. Post Comments
  3. Custom Meta boxes which consists of different video links, excel file links and pdfs links.
  4. Notes which user adds
  5. Spoken words in Videos

I have tried implementing Relevanssi Pro Search plugin for this since it searches values for custom meta fields as well. But, the result is not satisfactory at all.

Can anyone please suggest some good search plugin for this or do I need to go for some custom search solution for this?

Related posts

1 comment

  1. There is no easy way to do this without modifying the main search query that was generated by wordpress. I think you should go for posts_search & posts_join hook. They are located in wp-includes/query.php

    Do a var_dump first to see what you are getting & modify accordingly. Here’s some untested code i could put up right now to get you started.

    add_filter('posts_search', 'search_function', 10, 2);
    function search_function($search, $query) {
      if(is_admin() || !$query->is_main_query() || !$query->is_search)
        return; //determine if we are modifying the right query
    
      global $wpdb;
      $search_term = $query->get('s');
      $search = ' AND (';
    
      //point 1
      $search .= "($wpdb->posts.post_content LIKE '%$search_term%')";
    
      //need to add an OR between search conditions
      $search .= " OR ";
    
      //point 2
      $search .= "($wpdb->comments.comment_content LIKE '%$search_term%')";
    
      //need to add an OR between search conditions
      $search .= " OR ";
    
      //point 3
      $search .= "($wpdb->postmeta.meta_key = 'custom_field_key' AND $wpdb->postmeta.meta_value LIKE '%$search_term%')";
    
      //need to add an OR between search conditions
      $search .= " OR ";
    
      //point 4
      $search .= "({$wpdb->prefix}notes.text LIKE '%$search_term%')";
    
      //add the filter to join, sql will error out without joining the tables to the query
      add_filter('posts_join', 'join_tables');
    
      return $search . ') ';
    }
    
    function join_tables($join) {
      $join .= "JOIN $wpdb->comments ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)";
      $join .= "JOIN $wpdb->postmeta ON ($wpdb->postmeta.post_ID = $wpdb->posts.ID)";
      $join .= "JOIN {$wpdb->prefix}notes ON ({$wpdb->prefix}notes.post_ID = $wpdb->posts.ID)";
      return $join;
    }
    

Comments are closed.