0

i have the following schematic:

    the world
        - Cover the world.
    
    The country
        -Covered the country.
    
    theater
        - theater cover.

etc...

And I want that when I select any of the child categories the parent category appears.

my code:

add_filter('get_the_terms', 'hide_categories_terms', 10, 3);

function hide_categories_terms($terms, $post_id, $taxonomy){

    // define which category IDs you want to hide
    $excludeIDs = array(31, 32, 33, 34, 35, 36, 37, 38, 96);

    // get all the terms 
    $exclude = array();
    foreach ($excludeIDs as $id) {
        $exclude[] = get_term_by('id', $id, 'category');
    }

    // filter the categories
    if (!is_admin()) {
        foreach($terms as $key => $term){
            if($term->taxonomy == "category"){
                foreach ($exclude as $exKey => $exTerm) {
                    if($term->term_id == $exTerm->term_id) unset($terms[$key]);
                }
            }
        }
    }

    return $terms;
}

but just hide the child category.

1 Answer 1

0

This is what you want to achieve as I understand:

If a post is assigned to a child category, then remove it and show only its parent on the categories list of that post.

You can use the following code to achieve that for any child category:

add_filter('get_the_terms', function($terms, $postId, $taxonomy) {

    // Return early if not a category, there is an error, or we are on the dashboard.
    if('category' !== $taxonomy || is_wp_error($terms) || is_admin()) {
        return $terms;
    }

    // Create an array that will hold only parent categories
    $parents = [];

    // Loop on all the terms, keep the parents or get the parent of any child
    foreach ($terms as $term) {

        // This is a parent, add it and continue
        if(! $term->parent) {
            $parents[$term->term_id] = $term;
            continue;
        }

        // This is a child get its parent and add it
        $parent = get_term($term->parent, $taxonomy);
        if(is_a($parent, 'WP_Term')) {
            $parents[$parent->term_id] = $parent;
        }

    }

    // Finally, reset the array keys and return it
    return array_values($parents);

}, 10, 3);

But, if you want to do this for only a selected list of child categories, use this code:

add_filter('get_the_terms', function($terms, $postId, $taxonomy) {

    // Return early if not a category, there is an error, or we are on the dashboard.
    if('category' !== $taxonomy || is_wp_error($terms) || is_admin()) {
        return $terms;
    }

    // Categories to exclude
    $excludedIds = [31, 32, 33, 34, 35, 36, 37, 38, 96];

    // Create an array that will hold only not excluded categories
    $categoriesToKeep = [];

    // Loop on all the terms, and replace the excluded categories with their parents
    foreach ($terms as $term) {

        // This is not in the excluded list, add it and continue
        if(! in_array($term->term_id, $excludedIds)) {
            $categoriesToKeep[$term->term_id] = $term;
            continue;
        }

        // This an excluded category, replace it with its parent
        $parent = get_term($term->parent, $taxonomy);
        if(is_a($parent, 'WP_Term')) {
            $categoriesToKeep[$parent->term_id] = $parent;
        }

    }

    // Finally, reset the array keys and return it
    return array_values($categoriesToKeep);

}, 10, 3);
1
  • thanks!!!! this work por me <3 Commented Dec 15, 2021 at 13:40

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.