include custom fields in search query

I am trying to add custom fields to my search, here is my sql query:

add_filter( 'posts_where', 'title_filter', 100000, 2 );
function title_filter($where, &$wp_query){
        global $wpdb;

        if(isset($_GET['s'])){            
            $searchWord = trim($_GET['s']," ");
            $searchWhere = "AND ((
                             (wp_posts.post_title LIKE '% $searchWord %' OR wp_posts.post_title LIKE '$searchWord %' OR wp_posts.post_title LIKE '% $searchWord' OR wp_posts.post_title LIKE ' $searchWord' OR wp_posts.post_title LIKE '$searchWord' OR wp_posts.post_content LIKE '% $searchWord %' OR wp_posts.post_content LIKE '$searchWord %' OR wp_posts.post_content LIKE '% $searchWord' )
                            OR 
                            (tter.name LIKE '% $searchWord % ' OR tter.name LIKE '$searchWord %' OR  tter.name LIKE '% $searchWord')
                         OR 
                        (ttax.description LIKE '% $searchWord %' OR  ttax.description LIKE '$searchWord %' OR  ttax.description LIKE '% $searchWord')
                        OR 
                        (wp_posts.post_excerpt LIKE '% $searchWord %' OR wp_posts.post_excerpt LIKE '$searchWord %' OR wp_posts.post_excerpt LIKE '% $searchWord')
                       OR
                       (wp_postmeta.meta_value LIKE '% $searchWord %' OR wp_postmeta.meta_value LIKE '$searchWord %' OR wp_postmeta.meta_value LIKE '% $searchWord')

                ) AND 
                wp_posts.post_type IN ('post', 'page', 'attachment', 'reports', 'news-section', 'event-item', 'board-members', 'brand-logos', 'vacancies') AND ( wp_posts.post_status = 'future' OR wp_posts.post_author = 1 ) )";

    return $searchWhere ;


        }
        return $where; 
    }

Before adding this line OR (wp_postmeta.meta_value LIKE '% $searchWord %' OR wp_postmeta.meta_value LIKE '$searchWord %' OR wp_postmeta.meta_value LIKE '% $searchWord') my code was working perfectly for searching word from title or content but after adding this line not working, can’t understand why, because I guess I wrote right sql query.

Related posts

2 comments

  1. try to use: pre_get_posts action (in function.php)

    function filter_by_metavalue( $query ) {
        if ( $query->is_main_query() ) {
           $query->set('meta_key', 'YOUR KEY');
           $query->set('meta_value', 'VALUE');
        }}
    add_action( 'pre_get_posts', 'filter_by_metavalue' );
    
  2. Finally I found one solution, installed Relevanssi plugin, and also found one trick, wrote it in functions.php and now my custom fields are searchable:

    add_filter('the_content', 'my_the_content');
    function my_the_content($content) {
    relevant custom fields so that relevanssi includes their content (inlcuding the real my content if specified)
      if($post = is_the_content_called_by_relevanssi()){
    
        $fields = array('customfield2','customfield2'); 
        foreach($fields as $field){
          $field_value = get_post_meta($post->ID, $field, TRUE);
          $content .= ' '. ( is_array($field_value) ? implode(' ', $field_value) : $field_value );
        } 
      }
      else global $post;  
    
      return $content;
    }
    
    function is_the_content_called_by_relevanssi(){
      if(!function_exists('relevanssi_init')) return false;
    
      if (strnatcmp(phpversion(),'5.4.0') >= 0) 
        $trace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 21);
      else
        $trace = debug_backtrace();
    
      for($i = 0; $i<min(array(21,count($trace))); $i++){ 
        if('relevanssi_do_excerpt' == $trace[$i]['function']) return $trace[$i]['args'][0];
      }
      return FALSE; 
    }
    

Comments are closed.