2016-03-11 30 views
0
帖子

WordPress的 - 通過搜索分類

$args = array(
     'post_type' => 'post', 
     'taxonomy' => 'location', 
     'field' => 'name', 
     'posts_per_page' => 10, 
     'term' => $keyword, 
    ); 
$wp_query = new WP_Query($args); 

我的自定義WP搜索查詢我有2杆,它的位置是倫敦和牛津,倫敦。現在當我搜索倫敦則僅顯示1 **後(第1一)**。當我搜索牛津,然後它顯示沒有找到結果。

當我搜索牛津倫敦然後它的工作正常。

+0

在哪個函數中傳遞'args'數組? – Milap

+0

嗨米拉普我通過WP_Query –

+0

這個參數使用參數's'到您的參數來搜索您的關鍵字 – Jevuska

回答

2

Befor WP_Query運行編寫代碼

function advance_search_where($where){ 
    global $wpdb; 
    if (is_search()) 
     $where .= "OR (t.name LIKE '%".get_search_query()."%' AND {$wpdb->posts}.post_status = 'publish')"; 
    return $where; 
} 

function advance_search_join($join){ 
    global $wpdb; 
    if (is_search()) 
     $join .= "LEFT JOIN {$wpdb->term_relationships} tr ON {$wpdb->posts}.ID = tr.object_id INNER JOIN {$wpdb->term_taxonomy} tt ON tt.term_taxonomy_id=tr.term_taxonomy_id INNER JOIN {$wpdb->terms} t ON t.term_id = tt.term_id"; 
    return $join; 
} 

function advance_search_groupby($groupby){ 
    global $wpdb; 

    // we need to group on post ID 
    $groupby_id = "{$wpdb->posts}.ID"; 
    if(!is_search() || strpos($groupby, $groupby_id) !== false) return $groupby; 

    // groupby was empty, use ours 
    if(!strlen(trim($groupby))) return $groupby_id; 

    // wasn't empty, append ours 
    return $groupby.", ".$groupby_id; 
} 

add_filter('posts_where','advance_search_where'); 
add_filter('posts_join', 'advance_search_join'); 
add_filter('posts_groupby', 'advance_search_groupby'); 

現在運行你的WP_Query像這樣

$args = array(
     'post_type' => 'post', 
     'posts_per_page' => 10, 
     's' => $keyword, 
    ); 
$wp_query = new WP_Query($args); 

所有的結果後,你想你,如果你不只是刪除上述過濾堂妹刪除它,然後它將應用在該頁面的其他wp_query中。

remove_filter('posts_where','advance_search_where'); 
remove_filter('posts_join', 'advance_search_join'); 
remove_filter('posts_groupby', 'advance_search_groupby');