Skip to content

Instantly share code, notes, and snippets.

@Rahe
Forked from thierrypigot/search-filter.php
Last active August 29, 2015 14:10
Show Gist options
  • Save Rahe/5a4803b9a532a2a32351 to your computer and use it in GitHub Desktop.
Save Rahe/5a4803b9a532a2a32351 to your computer and use it in GitHub Desktop.
<?php
function custom_search_query( $query ) {
if( is_admin() && $query->is_main_query() && $query->query['post_type'] === 'revendeurs' && isset($query->query_vars['s']) )
{
$custom_fields = array(
// put all the meta fields you want to search for here
"raison_sociale",
);
$searchterm = $query->query_vars['s'];
// we have to remove the "s" parameter from the query, because it will prevent the posts from being found
$query->query_vars['s'] = "";
if ($searchterm != "") {
$meta_query = array('relation' => 'OR');
foreach($custom_fields as $key => $cf ) {
array_push($meta_query, array(
'key' => $cf,
'value' => $searchterm,
'compare' => 'LIKE'
));
}
$query->set("meta_query", $meta_query);
};
}
}
add_action( "pre_get_posts", "custom_search_query");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment