Skip to content

Instantly share code, notes, and snippets.

@thierrypigot
Created November 20, 2014 13:45
Show Gist options
  • Save thierrypigot/07ad0e801b6acb883b2f to your computer and use it in GitHub Desktop.
Save thierrypigot/07ad0e801b6acb883b2f 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 $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