Created
August 22, 2013 10:52
-
-
Save jnthnclrk/6305789 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment