Extend search to include custom post meta

This snippet extends WordPress search function to include custom post meta. Just put it in functions.php.

Code Snippet:

<?php

/*
 * Extend wp search to include custom post meta 
 */

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'
			)
		));
         $query->set('post_type', '__your_post_type__'); // optional
	};
}
add_filter( 'pre_get_posts', 'dc_custom_search_query');
?>

Posted in Pure hacks