1

How to list out all the terms of a custom taxonomy that are relative in another custom taxonomy?

I am creating a filter page for a CPT with multiple custom taxonomies.
Please see the screenshot below:

enter image description here

  • Custom Post Type: English "cpt_english"
  • Custom Taxonomy: Courses
  • Terms: course-a, course-b, course-c
  • Custom Taxonomy: Difficulties
  • Terms: easy, advanced , pro
  • Custom Taxonomy: Tasks "tasks"
  • Terms: task1, task2, task3, task4

The screen is html markup, not php generated code.

Question: How can I list out all the terms of a taxonomy based on another taxonomy? For example, The type: "Task 1" has "Difficulty": Easy, Advanced, Pro" But "Task 2" only has "Easy" and "Pro" ... so when clicking Task2, I do not want to show "Advanced" there, and Task 3 doesn't even has "Courses"... how can I achieve it with coding?

So what I meant is that, amount all the CPT that are associated with "Task1"(term name) of a custom taxonomy "task", these posts are also associated with term "easy", "advanced" and "pro" from another taxonomy "difficulty"

However, amount the CPT items associated with "Task2", none of them are associated with "advanced" ... so I do not want to list out "advanced" there

I know I can use "get_terms" and then "foreach" to list out all the terms of a taxonomy. But how I can "get_terms of taxonomy_a based on taxonomy_b" ?

1
  • What you're describing is called "faceted search", and is not possible with get_terms() alone. You will need to do a direct database query with $wpdb to get the correct terms. With a large number of terms this can get very complex and slow. The WordPress database just isn't structured well for this sort of thing. You might have an easier time using a plugin like FacetWP which indexes content in another database table whose structure is optimised for this sort of work. Jun 17, 2021 at 10:33

2 Answers 2

0

I am assuming your custom taxonomy is hierarchal and you are attempting to get the children within the hierarchy. If so this previous answer may help

Get the children of the parent category

And this wp func: https://developer.wordpress.org/reference/functions/get_term_children/

1
  • They are separate taxonomies, nothing to do with children terms
    – Atimmy
    Jun 16, 2021 at 19:03
0

Based on your problem, I researched and found the solution. https://stackoverflow.com/a/69483006/15424153

It works

Your Answer

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

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