I'm trying to interfere default WP search (?s=
) to make it search through posts custom fields.
Firstly I'm trying to debug my search with this piece of code in functions.php
:
function add_search_in_custom_fields( $query ) {
$query->set(
'post_type', 'my_post_type',
'meta_query', array(
array(
'key' => 'my_key_title',
'value' => 'my_value_title',
'compare' => 'LIKE'
)
)
);
return $query;
}
add_action( 'pre_get_posts', 'add_search_in_custom_fields' );
What's strange - that I'm getting all posts (with my_post_type
) - doesn't matter if there is value
or not in posts custom field.
Seems like $query
doesn't look at meta_query
parameters at all.
Any ideas?