2011-10-07 61 views
4

我有一個自定義後類型,稱爲portfolio,並呼籲build-type一個自定義分類(作爲類別)查詢職位

我試圖通過build-type ID例如查詢portfolio職位在「酒店」的所有投資組合的帖子(ID = 4的這種分類)

// gets the ID from a custom field to show posts on a specific page 
$buildType = get_post_meta($post->ID, 'build_type_id', true); 
// run query 
query_posts(array( 
    'post_type' => 'portfolio', 
    'showposts' => -1, 
    'tax_query' => array(
     'taxonomy' => 'build-type', 
     'terms' => $buildType, 
     'field' => 'term_id' 
    ), 
    'orderby' => 'title', 
    'order' => 'ASC' 
)); 

目前它調用所有portfolio職位,而不僅僅是那些與build-type ID

對於'field' => 'term_id'我應該使用term_idtag_IDid還是別的?

任何人都知道如何才能正常工作?

在此先感謝!

回答

14

我幫助解決它:https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id

tax-query需求是數組

的數組

最終的解決方案是:

// gets the ID from a custom field to show posts on a specific page 
$buildType = get_post_meta($post->ID, 'build_type_id', true); 
// run query 
query_posts(array( 
    'post_type' => 'portfolio', 
    'showposts' => -1, 
    'tax_query' => array(
     array(
      'taxonomy' => 'build-type', 
      'terms' => $buildType, 
      'field' => 'term_id', 
     ) 
    ), 
    'orderby' => 'title', 
    'order' => 'ASC') 
); 

GitHub上這裏:

https://gist.github.com/1275191

0

我不是WP-gury,我花了數小時試圖解決同樣的問題。最終我發現了這篇博客文章:http://richardsweeney.com/blog/wordpress-3-0-custom-queries-post-types-and-taxonomies/

答案有點半不好:顯然你不能過濾這樣的自定義帖子類型(這隻適用於帖子),這是一種遺憾!

我的工作是這樣的內容:

的$ args [ 'custom_tax'] = 'custom_tax_slug'; query_posts($ args);

希望它有幫助!

//麥克

+0

查看上面Mike,已解決 – mattberridge