This snippet will allow you to extend the WordPress search function to include custom post meta.
Instructions
Add the following code to your functions.php
<?php
function custom_search_query( $query ) {
if ( !is_admin() && $query->is_search ) {
$query->set('meta_query', array(
array(
'key' => '__meta_key__',
'value' => $query->query_vars['s'],
'compare' => 'LIKE'
)
));
// you can add additional parameters like a specific 'post_type'
// $query->set('post_type', 'project');
};
}
add_filter( 'pre_get_posts', 'custom_search_query');
?>
Comments