因此,事实证明,您实际上无法以这种方式将ACF/自定义字段添加到搜索查询中。这似乎迫使和进入了质疑。
我使用了多种来源的组合来创建下面的内容,现在效果非常好。
/* ADD META FIELD TO SEARCH QUERY */
// fields for Ajax search, admin and normal search
if (!isset($GLOBALS[\'current_screen\']) && !is_customize_preview()):
// DAMN AJAX! is_admin() is always true when using it ;-/
add_meta_field_to_search_query(\'loc_refid\');
add_meta_field_to_search_query(\'location_postcode_preview\');
add_meta_field_to_search_query(\'location_region\');
add_meta_field_to_search_query(\'location_areas\');
elseif (is_admin()):
add_meta_field_to_search_query(\'loc_refid\');
add_meta_field_to_search_query(\'location_postcode\');
add_meta_field_to_search_query(\'location_postcode_preview\');
add_meta_field_to_search_query(\'location_region\');
add_meta_field_to_search_query(\'location_areas\');
add_meta_field_to_search_query(\'location_map_position\' );
add_meta_field_to_search_query(\'user_organisation\');
add_meta_field_to_search_query(\'user_position\');
add_meta_field_to_search_query(\'client_location\');
add_meta_field_to_search_query(\'user_email\');
else:
add_meta_field_to_search_query(\'loc_refid\');
add_meta_field_to_search_query(\'location_postcode_preview\');
add_meta_field_to_search_query(\'location_region\');
add_meta_field_to_search_query(\'location_areas\');
endif;
function add_meta_field_to_search_query($field, $condition = "LIKE")
{
// echo "add_meta_field_to_search_query; field:$field";
if (isset($GLOBALS[\'added_meta_field_to_search_query\'])) {
$GLOBALS[\'added_meta_field_to_search_query\'][] = \'\\\'\' . $field . \'\\\'\';
return;
}
$GLOBALS[\'added_meta_field_to_search_query\'] = array();
$GLOBALS[\'added_meta_field_to_search_query\'][] = \'\\\'\' . $field . \'\\\'\';
add_filter(\'posts_join\', function ($join) {
// echo "$s \'posts_join\' <br />";
global $wpdb;
if (is_search()) {
$join .= " LEFT JOIN $wpdb->postmeta ON $wpdb->posts.ID = $wpdb->postmeta.post_id ";
}
return $join;
});
add_filter(\'posts_groupby\', function ($groupby) {
// echo "$s \'posts_groupby\' <br />";
global $wpdb;
if (is_search()) {
$groupby = "$wpdb->posts.ID";
}
return $groupby;
});
add_filter(\'posts_search\', function ($search_sql) {
global $wpdb;
$search_terms = get_query_var(\'search_terms\');
if (!empty($search_terms)) {
// echo "posts_search<br />";
// print_r($search_terms);
foreach ($search_terms as $search_term) {
// echo "search_term: $search_term<br />";
$old_or = "OR ({$wpdb->posts}.post_content LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\')";
$new_or = $old_or . " OR ({$wpdb->postmeta}.meta_value LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\' AND {$wpdb->postmeta}.meta_key IN (" . implode(\', \', $GLOBALS[\'added_meta_field_to_search_query\']) . "))";
$search_sql = str_replace($old_or, $new_or, $search_sql);
}
}
$search_sql = str_replace(" ORDER BY ", " GROUP BY $wpdb->posts.ID ORDER BY ", $search_sql);
return $search_sql;
});
// ajax seach only, basically same as above but checking for $GLOBALS[\'fd_search_term\'] which holds the search string
add_filter(\'posts_where\', function ($search_sql) {
global $wpdb;
// print_r($wpdb);
$search_terms = explode(\' \', $GLOBALS[\'fd_search_term\']);
// if (!empty($search_terms) && !isset($GLOBALS[\'current_screen\']) && !is_customize_preview() && !is_search()) {
if ($GLOBALS[\'fd_search_term\']) {
// echo "AJAX posts_search<br />";
// print_r($search_terms);
foreach ($search_terms as $search_term) {
// echo "search_term: $search_term<br />";
$old_or = "OR ({$wpdb->posts}.post_content LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\')";
$new_or = $old_or . " OR ({$wpdb->postmeta}.meta_value LIKE \'{$wpdb->placeholder_escape()}{$search_term}{$wpdb->placeholder_escape()}\' AND {$wpdb->postmeta}.meta_key IN (" . implode(\', \', $GLOBALS[\'added_meta_field_to_search_query\']) . "))";
$search_sql = str_replace($old_or, $new_or, $search_sql);
}
}
$search_sql = str_replace(" ORDER BY ", " GROUP BY $wpdb->posts.ID ORDER BY ", $search_sql);
return $search_sql;
});
}