仅在帖子标题中进行wordpress搜索

时间:2012-02-27 16:48:39

标签: wordpress

Hiii,

我有一个新的网站,我想修改wordpress mystique主题3.0,以便只搜索帖子标题!!

我该怎么做?

1 个答案:

答案 0 :(得分:41)

如果您在发布之前进行了搜索,那么google中有大量文章可供使用。
wordress search only in post 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($wpdb->esc_like($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', 500, 2);

将此代码添加到 functions.php