我对这段代码做了一些修复,因为它不再工作了。
以下代码将搜索与标题类似的术语OR 内容OR meta\\u值:
function custom_search_where( $where ) {
if ( is_search() ) {
global $wpdb;
// Overwrite the existing WHERE clause.
$where = \'\';
// Store all search terms into array.
$search_terms = explode( \' \', get_search_query() );
// Tables names.
$type = $wpdb->prefix . "posts.post_type";
$status = $wpdb->prefix . "posts.post_status";
$title = $wpdb->prefix . "posts.post_title";
$content = $wpdb->prefix . "posts.post_content";
$meta_value = $wpdb->prefix . "postmeta.meta_value";
foreach ( $search_terms as $term ) {
$term = trim($term);
$where .= " AND ( ($title LIKE \'%$term%\') OR ($content LIKE \'%$term%\') OR ($meta_value LIKE \'%$term%\') ) ";
}
// As WHERE clause is overwritten, you\'ll need to specify the post type, the status and/or anything else you need.
// Post Types.
$where .= " AND ($type IN (\'post\', \'page\', ... )) ";
// Post status.
$where .= " AND ($status = \'publish\') ";
}
return $where;
}
add_filter(\'posts_where\', \'custom_search_where\', 999);
以及
function custom_search_join ($join) {
if( is_search() ) {
global $wpdb;
$join .= " INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)";
}
return $join;
}
add_filter(\'posts_join\', \'custom_search_join\' );