SO网友:Md. Mrinal Haque
在函数中添加以下代码。php自定义WP搜索title
仅限
function search_by_title_only( $search, &$wp_query ) {
global $wpdb;
if ( empty( $search ) )
return $search; // skip processing - no search term in query
$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\', \'search_by_title_only\' );
<在中搜索
post content
是WP中的默认搜索选项