How to get custom taxonomy terms based on another taxonomy?

admin2025-01-07  4

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:

  • 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" ?

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:

  • 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" ?

Share Improve this question asked Jun 16, 2021 at 18:13 AtimmyAtimmy 557 bronze badges 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. – Jacob Peattie Commented Jun 17, 2021 at 10:33
Add a comment  | 

3 Answers 3

Reset to default 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/

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

It works

you can use the get_terms() function along with a custom query to retrieve terms from one taxonomy based on the terms selected in another taxonomy.

Not Tested Code

// Assuming you have the selected term from the "Tasks" taxonomy
$selected_task = 'task2'; // Replace this with the selected task

// Get the term object for the selected task
$task_term = get_term_by('slug', $selected_task, 'tasks');

if ($task_term) {
    // Get the associated terms from the "Difficulties" taxonomy for the selected task
    $difficulty_terms = get_the_terms($task_term->term_id, 'difficulties');
    
    if ($difficulty_terms && !is_wp_error($difficulty_terms)) {
        // Extract the term slugs from the difficulty terms
        $difficulty_slugs = wp_list_pluck($difficulty_terms, 'slug');
        
        // Get the terms from the "Courses" taxonomy associated with the selected difficulties
        $course_terms = get_terms(array(
            'taxonomy' => 'courses',
            'hide_empty' => false,
            'meta_query' => array(
                array(
                    'key' => 'difficulty', // Assuming a custom field name for difficulty
                    'value' => $difficulty_slugs,
                    'compare' => 'IN',
                ),
            ),
        ));
        
        if ($course_terms && !is_wp_error($course_terms)) {
            // Output the list of course terms
            echo '<ul>';
            foreach ($course_terms as $course_term) {
                echo '<li>' . $course_term->name . '</li>';
            }
            echo '</ul>';
        } else {
            echo 'No course terms found.';
        }
    } else {
        echo 'No difficulty terms found for the selected task.';
    }
} else {
    echo 'Invalid task selected.';
}

We first get the term object for the selected task from the Tasks taxonomy. Then, we retrieve the associated difficulty terms for the selected task. After that, we extract the slugs of the difficulty terms. Finally, we use a custom query to get the course terms associated with the selected difficulties from the Courses taxonomy.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736255957a323.html

最新回复(0)