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.
try to use: pre_get_posts action (in function.php)
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: