How to query related products only from custom texonomy/category

Hi, I’m back again with the same topic - related products. I know that you can now query related products by clicking on it in the woocommerce section in query. But I need to list only those products that share the category I indicated. How do I do that?

Browser: Chrome 110
OS: macOS / Windows / Linux / etc.
URL: Link to a page that illustrates this issue
Video: Short screen recording that illustrates this issue (free tool: jam.dev)

[Please describe this bug in as much detail as possible so we can replicate & debug this bug]

Hi @pedronx,

I don’t see a bug here :thinking:, so I’ve moved it to the How To topic.

By default, it will show you products that are inside the same category or tag, but if you want to have anything custom, then you can create new custom query loop, and manually select which category you want.

Matej

Ok, so how to do it? Any of the solutions proposed on this forum didn’t work.

What did you try up untill now?

:slight_smile:
I tried:

  1. query childs of specified term (100 in my case is the category id)

  2. select Related products in Woocommerce section of query buider, and add id to “Child of” input

  3. add taxonomy query (slug, term_id and name doesn’t work) and add id to “child of” input.

Why this query doesn’t display anything? Is this a bug?


Hi,

so, to show products only from one category, you can use “Terms (include)” field:

The setup below will show you all products that are inside the clothing category.

Available since 2.0 alpha version, you can also use Taxonomy Query, with the “Terms” set to {post_terms_product_cat:plain:term_id}. That way, you don’t need to manually select the category (like above), but you can query products dynamically, based on current product categories.


(Mentioned brefly in the academy: Dynamic Data – Bricks Academy)

If you don’t use 2.0 alpha, then you can use the workaround that was provided here in this forum topic: SOLVED: Get related products (comma into category name) - #2 by itchycode

Let me know if this helps.
Matej

Thank You!
It’s live site, so i don’t use 2.0 alpha.

“so, to show products only from one category, you can use “Terms (include)” field:” - I need to show products, that belongs to specific category (age) subcategories. My structure looks like this:

  • Age
      • 0-3
      • 3-5

      • There is a lot more categories assigned to product, but i only need to query this specific category subcategories.
        So, when user browse product that belongs to sucategory 0-3, i need to display products from that subcategory. If product is asigned also to 3-5 subcategory, i need to display both subcategories (in one query).

I hope that this clarify scenerio :slight_smile:

ps 1. this solution didn;t work:

My setup:
image

ps 2. what is the field “child of” used for?
image

Did you allow the execution of the function? If you ouput the {echo:....} inside the basic text element, what is the output?

For your second question, it’s used for returning only posts from specific child. So, if your category “Age” has ID of 123, and you want to list all subcategories, you will add “123” to the custom term query loop.

Matej

Did you allow the execution of the function? - no… Thank’s for pointing out - now the query outputs the results :slight_smile:

For your second question, it’s used for returning only posts from specific child - unfortunately, when I add the category id to the query, in the “child of” position, the query loop does not work again and does not display the results.

Perfect, I’m glad that it’s working.

Yes, because you are querying the posts, but the ID you added to the “child of” field is from terms/category.
They need to be the same type, then it should work :slight_smile:

Perfect, I’m glad that it’s working.

I was in a hurry, it doesn’t work. And after thinking about it, it couldn’t have worked, because I didn’t give priority anywhere to the selected category that I care about.
Thinking further, how is this custom function different from the native functionality of the “Related products” checkbox? Probably nothing, but it displays different results…

The perfect scenario is where I can click the “related products” checkbox and select a category to prioritize, like in the image below. Unfortunately, this configuration doesn’t display anything.

Yes, because you are querying the posts, but the ID you added to the “child of” field is from terms/category. They need to be the same type, then it should work :slight_smile:

Since I’m querying posts of the product type, the “child of” option shouldn’t be available, because a product can’t have a parent. This is misleading, or I don’t understand something.