我有一个名为usr\\u jardin的帖子类型,其中有一个名为sim\\u nuip的自定义字段。我想让搜索工作的标题的职位,也由自定义字段。我试过了,但没用。
function searchfilter($query)
{
$custom_fields = array("_post_title", "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"]);
}