2009-07-20 185 views
4

我創建了一個名爲'technologies'的自定義分類法,但無法像使用類別或標籤一樣查詢多個術語。查詢Wordpress 2.8中的多個自定義分類術語?

這些querys做的工作:

query_posts('tag=goldfish,airplanes'); 

query_posts('technologies=php'); 

然而,無論是以下工作正確的:

query_posts('technologies=php,sql'); 

query_posts('technologies=php&technologies=sql'); 

我的目標:顯示所有帖子與 'PHP' 的技術,所有帖子的技術'sql'

任何想法?這甚至有可能嗎?謝謝!

回答

10

顯然query_posts不能在這個具體情況幫助。 (希望這將在WordPress的未來版本中加入!)的解決方案是使用像下面這樣的自定義選擇查詢:

SELECT * 
FROM $wpdb->posts 
LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id) 
LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) 
LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id) 
WHERE $wpdb->posts.post_type = 'post' 
AND $wpdb->posts.post_status = 'publish' 
AND $wpdb->term_taxonomy.taxonomy = 'technologies' 
AND $wpdb->terms.slug = 'php' OR $wpdb->terms.slug = 'css' 
ORDER BY $wpdb->posts.post_date DESC 

更多信息可以在WordPress的法典中找到: http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

+0

像上面這樣的手動查詢正是我最終使用的。現在,如果您不想一個接一個地運行多個get_posts()查詢(並且不這樣做),那麼這是唯一的方法。 – bobsoap 2010-08-16 03:00:09

+0

這個查詢看起來可能適用於我想要做的事情,但是如果我想在一篇文章中搜索多個詞彙,該怎麼辦?我已經嘗試將OR更改爲AND(查詢的第9行),它似乎不起作用。 – jasonaburton 2012-08-13 12:27:50

0

這是某種程度上愚蠢的是,在WP中實現自定義分類標準之後,沒有任何內置函數可以隨意使用它們,並且文檔接近於不存在。我正在尋找一個解決方案,這個查詢解決了它(並使我的一天)。謝謝。

不過,可悲的是我太愚蠢(OOP盲目)使它成爲一個函數,所以我不再重複一遍。 我得到:**Fatal error**: Call to a member function get_results() on a non-object

我想我不知道如何從一個函數中調用WPDB $

+0

我不得不添加「全球$ wpdb;」在函數內部。 傻我。在發佈上面的條目之後就意識到了。 – pax 2009-10-11 23:32:13

1

好的,所以這裏是我的破解。這有點哈克,但它的作品。最大的缺點是需要重新添加任何其他查詢變量,因爲在調用多個術語時,失敗會消除所有查詢變量。

此外,我沒有對多個分類法中的查詢進行測試。這隻適用於特定的分類。使用風險自負。

function multi_tax_terms($where) { 
    global $wp_query; 
    if (strpos($wp_query->query_vars['term'], ',') !== false && strpos($where, "AND 0") !== false) { 
     // it's failing because taxonomies can't handle multiple terms 
     //first, get the terms 
     $term_arr = explode(",", $wp_query->query_vars['term']); 
     foreach($term_arr as $term_item) { 
      $terms[] = get_terms($wp_query->query_vars['taxonomy'], array('slug' => $term_item)); 
     } 

     //next, get the id of posts with that term in that tax 
     foreach ($terms as $term) { 
      $term_ids[] = $term[0]->term_id; 
     } 

     $post_ids = get_objects_in_term($term_ids, $wp_query->query_vars['taxonomy']); 

     if (!is_wp_error($post_ids) && count($post_ids)) { 
      // build the new query 
      $new_where = " AND wp_posts.ID IN (" . implode(', ', $post_ids) . ") "; 
      // re-add any other query vars via concatenation on the $new_where string below here 

      // now, sub out the bad where with the good 
      $where = str_replace("AND 0", $new_where, $where); 
     } else { 
      // give up 
     } 
    } 
    return $where; 
} 

add_filter("posts_where", "multi_tax_terms"); 
0

它應該是這樣的:

 global $wp_query; 
     query_posts(
       array_merge(
        array('taxonomy' => 'technologies', 'term' => array('sql', 'php')), 
        $wp_query->query 
       ) 
      ); 

,對於定製post_types至少工作。

0

嘿,我也面臨着同樣的問題一次。如果你沒有很多多個值,那麼你就可以做到這一點通過以下方式,而不是寫一個原始的SQL查詢:

$loop = new WP_Query(array('technologies' => 'php','technologies' => 'sql')); 

然後你就可以遍歷wp_query對象在這裏創建。 雖然這是一個非常古老的帖子,並且您確定已經解決了這個問題。 :)

6

這是一個延遲迴復,但它在谷歌目前首次出現「多個wordpress相關帖子」,所以我認爲我會貢獻自己的發現。

由於此問題已發佈Wordpress已更改爲允許此類查詢。這會給你任何分配給對象的自定義分類方面的相關帖子的列表:

$post_cats = wp_get_object_terms(get_the_ID(), 'video_category', array('fields' => 'ids')); 

$args=array(
    "tax_query" => array(
     array(
      "taxonomy" => "video_category", 
      "field" => "id", 
      "terms" => $post_cats 
     ) 
    ), 
    'post__not_in' => array(get_the_ID()), 
    'post_type' => 'video', 
    'posts_per_page' => 8, 
    'caller_get_posts' => 1 
); 

$related_by_cats = new WP_Query($args); 

這是我的第一個貢獻,所以,我希望這是達到標準。

0
//equivalent to get_posts 
function brand_get_posts($args=array()){ 
global $wpdb; 
$sql = "SELECT p.* "; 
$sql.= " FROM $wpdb->posts p"; 
$sql.= " LEFT JOIN $wpdb->term_relationships term_r ON(p.ID = term_r.object_id)"; 
$sql.= " LEFT JOIN $wpdb->term_taxonomy term_t ON(term_r.term_taxonomy_id = term_t.term_taxonomy_id)"; 
$sql.= " LEFT JOIN $wpdb->terms terms ON(term_t.term_id = terms.term_id)"; 
$sql.= " WHERE 1=1 "; 
if(!empty($args['post_type'])){ 
    $sql.= " AND p.post_type = '".$args['post_type']."'"; 
} 
$sql.= " AND p.post_status = 'publish'"; 

if(!empty($args['taxonomy'])){ 
    $sql.= " AND term_t.taxonomy = '".$args['taxonomy']."'"; 
} 
if(!empty($args['terms'])&&is_array($args['terms'])){ 
    $sql.= " AND terms.slug IN ('".implode(",",$args['terms'])."')"; 
} 
$sql.= " ORDER BY p.post_date DESC"; 
if(!empty($args['posts_per_page'])){ 
    $sql.=" LIMIT ".$args['posts_per_page']; 
} 
if(!empty($args['offset'])){ 
    $sql.=" OFFSET ".$args['offset']; 
} 
//echo '<h1>'.$sql.'</h1>'; 
return $wpdb->get_results($sql); 
} 
相關問題