Custom post taxonomies as tax_query terms?
I have these two custom post types called “product” and “reference”. On “references” I have attached a custom taxonomy called “product_reference” which I use to specify which products the references are related to. On the “product” custom posts I have attached a custom module called “module-relevant-references” for fetching reference-posts relevant to the product.
This might sound a bit confusing as a whole so I have included a simplified picture to outline the setup.
What I need to do is to gather three relevant “reference” posts into the “relevant references” module (most likely with wp_query’s tax_query
filter parameter), but I haven’t been able to achieve this the intended way.
The filter works if I fill in the tax_query
terms manually in the wp_query, like in the code snippet below (product_taxonomy 1, 2 & 3):
$the_query = new WP_Query(array(
'post_type' => 'reference',
'post_status' => 'publish',
'post__not_in' => array( get_the_ID() ),
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 3,
'tax_query' => array(
array(
'taxonomy' => 'product_reference',
'field' => 'slug',
'terms' => array(
'taxonomy1',
'taxonomy2',
'taxonomy3'
),
'hide_empty' => true,
'include_children' => true,
'operator' => 'IN'
),
)
));
However I need this to work dynamically with the “product_reference” taxonomies attached to the reference posts by getting the taxonomies from the reference as an array. The relevant-references module and the reference template are on different files which makes it challenging for me.
I’ve tried using get_the_terms
wp-function on the module php file, but it is unable to gather the information from the reference and returns “bool(false)”.
var_dump(get_the_terms(get_the_ID(), 'product_reference'));
wp_get_object_terms
or wp_get_post_terms
didn’t seem to do the trick either and both just returned array(0) { }.
So my question is: How do I get the “product_reference” taxonomies from the references and include them as an array for the tax_query
terms in the module? Is this even possible with the current setup?
I’m open for completely different approaches as well but at this point I’d really prefer not rebuilding the whole structure if possible.
Thanks for any help!
Leave an answer