在funcitons.php中添加如下代码即可
// 只搜索文章标题
function mi_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
if(empty($search)) {
return $search; // skip processing - no search term in query
}
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
var_dump($search);
$search = array();
foreach ( ( array ) $q['search_terms'] as $term ){
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
}
if ( ! is_user_logged_in() ){
$search[] = "$wpdb->posts.post_password = ''";
}
$search = ' AND ' . implode( ' AND ', $search );
}
var_dump($search);
return $search;
}
add_filter( 'posts_search', 'mi_search_by_title', 10, 2 );