1

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. 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!

4
  • 1
    Note that terms and taxonomies are not the same thing, but you refer to both of them with the word 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
    – Tom J Nowell
    Commented Mar 25, 2020 at 16:13
  • Is your question: "How do I fetch posts that have one or more of these terms A/B/C?"
    – Tom J Nowell
    Commented Mar 25, 2020 at 16:14
  • Thank you for the quick reply! You're right, I managed to mix up couple of terms here and there. Made some fixes so hopefully makes more sense now. Essentially yes - that is my question. However I couldn't find any working solutions by Googling something similar, which made me beg the question; is the current setup illogical and should it be perhaps restructured? Hopefully it doesn't have to come to that, so if you have any other suggestions I will gladly try them.
    – AlluP
    Commented Mar 26, 2020 at 7:05
  • Nevermind, I found an alternative solution for this problem. I just utilized the product page title as the tax_query's term since it just happens to be identical to the term I'm supposed to use. Not the best or prettiest solution by any means, but unfortunately I have to time to figure out this puzzle since the case is quite urgent. Learned a lot nonetheless.
    – AlluP
    Commented Mar 27, 2020 at 6:47

1 Answer 1

0

There is an example of how to do this on the official doc for WP_Query:

https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

Specifically, by declaring multiple items in the tax_query and setting their relation:

Display posts that are in the quotes category OR have the quote post format:

$args = array(
    'post_type' => 'post',
    'tax_query' => array(
        'relation' => 'OR',
        array(
            'taxonomy' => 'category',
            'field'    => 'slug',
            'terms'    => array( 'quotes' ),
        ),
        array(
            'taxonomy' => 'post_format',
            'field'    => 'slug',
            'terms'    => array( 'post-format-quote' ),
        ),
    ),
);
$query = new WP_Query( $args );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.