Wordpress - Get Posts Under Custom Taxonomy
Your tax query is incorrect, field
should be the field you want to query on: term_id
, name
, or 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,
)
)
)
);
You should use get_terms()
for taxonomies when you can.
<?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->term_id,
)
)
)
);
print_r( $posts_array );
}
?>
Link to Codex: http://codex.wordpress.org/Function_Reference/get_terms