软件下载 | 资讯教程 | 最近更新 | 下载排行 | 一键转帖 | 发布投稿
您的位置:最火下载站 > 建站程序 > Blog教程 > 代码实现WordPress限制搜索文章标题

代码实现WordPress限制搜索文章标题

使用这段代码,将在网站的搜索中限制搜索范围,只搜索文章的标题,而不是全文搜索。至于用处大不大,那就见仁见智了。

用法一样是放在functions.php中。

Copy to ClipboardLiehuo.Net Codes引用的内容:[www.veryhuo.com]
function __search_by_title_only( $search, &$wp_query )
{
if ( empty($search) )
return $search; // skip processing - no search term in query
$q =& $wp_query->query_vars;
// wp-includes/query.php line 2128 (version 3.1)
$n = !empty($q['exact']) ? '' : '%';
$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 ';
}
$term = esc_sql( like_escape( $q['s'] ) );
if ( empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
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', 10, 2 );
    相关阅读
    网友评论