I have a custom post type called portfolio
and a custom taxonomy called build-type
(acting as categories)
I am trying to query portfolio
posts by build-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 the build-type
ID
For 'field' => 'term_id'
should I be using term_id
, tag_ID
, id
or something else?
Anyone know how to get this working?
Thanks in advance!
The reason this isn’t working is because ‘tax_query’ needs to be an array of arrays (confusing, I know).
It is that way so you can group a few different rules together.
Drew was right,
tax-query
needs to be an array of arraysThe final solution is:
On github here:
https://gist.github.com/1275191
Thanks!
You need to create an array inside tax_query where you can also select operators.
For example a print_r of tax_query should look like these.
Of course you can change the field for id but i always used slugs to keep it simpler.
As you can see you can query multiple taxonomies like these.
you don’t need to use the tax_query,remove it and append the following:
term=>build-type,'build-type=>hotels'
for example.