Skip to content

Instantly share code, notes, and snippets.

@not-only-code
Created June 7, 2012 15:47
Show Gist options
  • Save not-only-code/2889579 to your computer and use it in GitHub Desktop.
Save not-only-code/2889579 to your computer and use it in GitHub Desktop.
adds meta query on Wordpress search
/**
* adds meta values on search query
*
* @param object $query
*
**/
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 params like a specific 'post_type'
// $query->set('post_type', 'project');
};
}
add_filter( 'pre_get_posts', 'custom_search_query');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment