I have 2 postmeta english_title and english_content for each post. When user search something, plus post tile and post content, I need it search on this two meta keys too. I use this function
function custom_search_query( $query ) {
// Ensure it is a search query and the main query
if ( $query->is_search() && $query->is_main_query() ) {
// Get the search term
$search_terms = $query->query_vars['s'];
// Add meta query for searching custom fields
$meta_query = [
'relation' => 'OR',
[
'key' => 'english_title',
'value' => '%' . $search_terms . '%',
'compare' => 'LIKE',
],
[
'key' => 'english_content',
'value' => '%' . $search_terms . '%',
'compare' => 'LIKE',
],
];
// Set the meta query for custom fields
$query->set( 'meta_query', $meta_query );
}
}
add_action( 'pre_get_posts', 'custom_search_query' );
I am using them by default with no plugin, but when I use this function, nothing shows up, even if the keyword is in the post title. there is nothing on the debug file.
could you tell me how can debug it to solve the problem