这里有一个过滤器可以解决这个问题。将其放入主题中functions.php
或插件。
/**
* Search SQL filter for matching against post title only.
*
* @link http://wordpress.stackexchange.com/a/11826/1685
*
* @param string $search
* @param WP_Query $wp_query
*/
function wpse_11826_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars[\'search_terms\'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q[\'exact\'] ) ? \'\' : \'%\';
$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 );
}
return $search;
}
add_filter( \'posts_search\', \'wpse_11826_search_by_title\', 10, 2 );
大多数代码只是从
WP_Query
类,但使用
post_content LIKE
\'s已删除。
UPDATE: 已删除已弃用like_escape()
自4.0以来