2012-11-21 158 views

回答

11

您可以使用篩選器掛鉤來修改搜索查詢以加入分類表。

例如同時搜索上的「作者」分類

首先加入分類表

function tax_search_join($join) 
{ 
    global $wpdb; 
    if(is_search()) 
    { 
    $join .= " 
     INNER JOIN 
      {$wpdb->term_relationships} ON {$wpdb->posts}.ID = {$wpdb->term_relationships}.object_id 
     INNER JOIN 
      {$wpdb->term_taxonomy} ON {$wpdb->term_taxonomy}.term_taxonomy_id = {$wpdb->term_relationships}.term_taxonomy_id 
     INNER JOIN 
      {$wpdb->terms} ON {$wpdb->terms}.term_id = {$wpdb->term_taxonomy}.term_id 
     "; 
    } 
    return $join; 
} 
add_filter('posts_join', 'tax_search_join'); 

然後找到在分類學「作者」

function tax_search_where($where) 
{ 
    global $wpdb; 
    if(is_search()) 
    { 
    // add the search term to the query 
    $where .= " OR 
    (
     {$wpdb->term_taxonomy}.taxonomy LIKE 'author' 
     AND 
     {$wpdb->terms}.name LIKE ('%".$wpdb->escape(get_query_var('s'))."%') 
    ) "; 
    } 
    return $where; 
} 
add_filter('posts_where', 'tax_search_where'); 

最後組以郵寄的結果與搜索詞id以避免重複結果,因爲加入

function tax_search_groupby($groupby) 
{ 
    global $wpdb; 
    if(is_search()) 
    { 
    $groupby = "{$wpdb->posts}.ID"; 
    } 
    return $groupby; 
} 
add_filter('posts_groupby', 'tax_search_groupby'); 
相關問題