Query posts by custom taxonomy ID
44,460
I solved it with help from: https://wordpress.stackexchange.com/questions/30476/query-posts-by-custom-taxonomy-id
tax-query
needs to be an array of arrays
The final solution is:
// 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' )
);
On github here:
https://gist.github.com/1275191
Author by
Admin
Updated on July 05, 2022Comments
-
Admin almost 2 years
I have a custom post type called
portfolio
and a custom taxonomy calledbuild-type
(acting as categories)I am trying to query
portfolio
posts bybuild-type
ID e.g. all Portfolio posts in "Hotels" (id=4 for that taxonomy)// 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' ));
Currently it's calling all
portfolio
posts and not just those with thebuild-type
IDFor
'field' => 'term_id'
should I be usingterm_id
,tag_ID
,id
or something else?Anyone know how to get this working?
Thanks in advance!