2011-10-07 77 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