カスタムタクソノミー(fabric_building_types
)の下の投稿が取得できません。cat_idと
cat->name`も取得していますが、投稿を取得することができません。
$args = array(
'type' => 'post',
'child_of' => 0,
'parent' => '',
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 1,
'hierarchical' => 1,
'exclude' => '',
'include' => '',
'number' => '',
'taxonomy' => 'fabric_building_types',
'pad_counts' => false
);
$categories = get_categories( $args );
foreach ( $categories as $cat ) {
// here's my code for getting the posts for custom post type
$posts_array = get_posts(
array(
'showposts' => -1,
'post_type' => 'fabric_building',
'tax_query' => array(
array(
'taxonomy' => 'fabric_building_types',
'field' => $cat->cat_ID,
'terms' => $cat->name,
)
)
)
);
print_r( $posts_array );
}
誰か助けてください...ありがとうございます。
fieldには、クエリを実行するフィールドを指定してください。term_id
, name
, または slug
です。
$posts_array = get_posts(
array(
'posts_per_page' => -1,
'post_type' => 'fabric_building',
'tax_query' => array(
array(
'taxonomy' => 'fabric_building_types',
'field' => 'term_id',
'terms' => $cat->term_id,
)
)
)
);
タクソノミに対しては、できる限りget_terms()
を使うべきです。
<?php
/* Add your taxonomy. */
$taxonomies = array(
'fabric_building_types',
);
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true,
'exclude' => array(),
'exclude_tree' => array(),
'include' => array(),
'number' => '',
'fields' => 'all',
'slug' => '',
'parent' => '',
'hierarchical' => true,
'child_of' => 0,
'get' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false,
'offset' => '',
'search' => '',
'cache_domain' => 'core'
);
$terms = get_terms( $taxonomies, $args );
foreach ( $terms as $term ) {
// here's my code for getting the posts for custom post type
$posts_array = get_posts(
array( 'showposts' => -1,
'post_type' => 'fabric_building',
'tax_query' => array(
array(
'taxonomy' => 'fabric_building_types',
'field' => term_id,
'terms' => $term->name,
)
)
)
);
print_r( $posts_array );
}
?>
Codexへのリンク: http://codex.wordpress.org/Function_Reference/get_terms
global $post; $id = $post->ID;
$cat = get_the_category($id);
$loc = get_the_terms($id, 'taxonomy');
$posts = get_posts(
array('post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
'category' => $cat[0]->term_id,
'tax_query' => array(
array(
'taxonomy' => 'location',
'field' => 'id',
'terms' => $loc[0]->term_id,
)
)
)
);
print_r($posts);
のようになるはずです。