I have clothes products. One product is in multiples categories, like "gender" ("men" or "women") and "type" (such as "pants", "shirts", etc.).
I want to list all categories and subcategories of "type" from products that exist in "men" category.
ID of "type" category: 1696
$args = array(
'taxonomy' => 'product_cat',
'orderby' => 'title',
'order' => 'ASC',
'child_of' => 1696,
);
$categories = get_terms( $args );
print_r($categories);
That code gives me all the categories under 1696, but I would like to get only the "type" categories from products that also are in "men" category.
Am I clear?
Thanks a lot for your help.
2
Answers
Thanks for your help, but it's not what I need.
I found a way to achieve what I want. There must be a better way but here's how I did:
I'm looping every products and put them in arrays if they are in the gender category ("femme" and "homme").
After that, I'm formating the array to get this structure of categories (one array per gender): array('term_id' => 'name_id', 'term_id' => 'name_id', 'term_id' => 'name_id');
After that, I'm looping through all clothes categories and looking if this category ID exist in the array created just before. If yes, I'm adding this category to the menu.
Just for everyone to understand, I'm calling this code inside a function called by:
And here is the _custom_nav_menu_item function:
Thanks again for your help. :)
Since wordpress only allows one id at a time for
child_of
argument, you could create a custom array and runget_terms
for every parent category that you want.Let me know if that’s what you were looking for!