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 filtering with WP_Query
works if I fill in the tax_query
terms manually, like in the code snippet below (product_category 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(
'product_category1',
'product_category2',
'product_category3'
),
'hide_empty' => true,
'include_children' => true,
'operator' => 'IN'
),
)
));
However I need this to work dynamically by getting the “product_reference” terms from all the relevant references as an array. The relevant-references module and the reference template are on different files.
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: What is the correct way with this setup to fetch the relevant reference posts by filtering with “product_reference” terms?
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!
taxonomy
, which makes your question incredibly confusing. Can you edit it so that all references to terms are clearly marked as terms, and that the two terms are not muddled up? It's really difficult to follow what you mean. Use the edit link under the tags