要从搜索中删除元素,请使用post\\u search筛选器修改搜索的SQL查询,例如仅拉入标题。
function ni_search_by_title_only( $search, &$wp_query ){
global $wpdb;
if ( empty( $search ) )
return $search;
$q = $wp_query->query_vars;
$n = ! empty( $q[\'exact\'] ) ? \'\' : \'%\';
$search =
$searchand = \'\';
foreach ( (array) $q[\'search_terms\'] as $term ) {
$term = esc_sql( like_escape( $term ) );
$search .= "{$searchand}($wpdb->posts.post_title LIKE \'{$n}{$term}{$n}\')";
$searchand = \' AND \';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() )
$search .= " AND ($wpdb->posts.post_password = \'\') ";
}
return $search;
}
add_filter( \'posts_search\', \'ni_search_by_title_only\', 500, 2 );
摘自
https://nathaningram.com/restricting-wordpress-search-to-titles-only/就我个人而言,我更喜欢这个插件,因为它可以让我完全控制搜索中需要的每个字段,我使用的是relevanssi. 它应该很容易做到你想要的。