Skip to content

Instantly share code, notes, and snippets.

@dsebao
Last active December 19, 2015 06:39
Show Gist options
  • Save dsebao/5912710 to your computer and use it in GitHub Desktop.
Save dsebao/5912710 to your computer and use it in GitHub Desktop.
Agregar custom meta data al buscador de WordPress
<?php
function custom_search_query( $query ) {
$custom_fields = array(
//agregamos los valores de los custom meta data
"_post_title",
"custom_meta1",
"custom_meta2"
);
$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);
};
}
add_filter( "pre_get_posts", "custom_search_query");
add_action( "save_post", "add_title_custom_field");
function add_title_custom_field($postid){
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