How to Extend WordPress Search to Include Custom Post Meta
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
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');
function custom_search_query( $query ) {
$custom_fields = array(
// put all the meta fields you want to search for here
"_post_title",
"text_english",
"text_deutsch"
);
$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_filter( "pre_get_posts", "custom_search_query");
add_action( "save_post", "add_title_custom_field");
function add_title_custom_field($postid){
// since we removed the "s" from the search query, we want to create a custom field for every post_title. I don't use post_content, if you also want to index this, you will have to add this also as meta field.
update_post_meta($postid, "_post_title", $_POST["post_title"]);
}
as you remove the ‘s’ from the query. when we search any data, it show there empty like
Search Results for “”
So what is the solution for this, so that it not show it empty.
Good plugin to search by meta fields and taxonomies with built-in meta fields constructor http://codecanyon.net/item/wordpress-meta-data-filter/7002700?ref=realmag777
Hi. I have a post type called usr_jardin with a custom field called sim_nuip. I would like to get the search to work for the title of the post and also by custom field . I try this but does not work me.
function searchfilter($query) {
$custom_fields = array(
// put all the meta fields you want to search for here
“_post_title”,
“sim_nuip”
);
$searchterm = $query->query_vars[‘s’];
$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);
};
if ($query->is_search) {
$query->set(‘post_type’,array(‘post’,’usr_jardin’));
} return $query;
}
add_filter(‘pre_get_posts’,’searchfilter’);
add_action( “save_post”, “add_title_custom_field”);
function add_title_custom_field($postid){
update_post_meta($postid, “_post_title”, $_POST[“post_title”]);
}
e.g.
array_push($meta_query, array(
‘keys’ => $prefix. ‘store’,$prefix. ‘add’,$prefix. ‘zip’,$prefix. ‘city’,$prefix . ‘tel’,$prefix . ‘fax’,$prefix . ’email’,$prefix. ‘owner’,
‘value’ => $searchterm,
‘compare’ => ‘LIKE’
));
if u use ‘keys’ then u insert multiple keys in this function
What kind of code is that? Completely full of errors! You should fix this up.
+1
Code-/Markup is cleaned up .. \m/