Taxonomy – Delete term and all it's subterms

Hi,

I’m writing a plugin that involves a large hierarchical taxonomy that is 5 levels deep. Users should be able to turn parts of the taxonomy on/off depending on what is relevant to them.

It’s all going fine, taxonomy & terms defined with checkboxes in a settings page for what parts they want to use. But now I’m struggling with how to remove a term and all it’s subterms if a checkbox is first ticked then unticked.

The function wp_delete_term is fine for removing one term but if a term has children the child terms get added as children of the removed terms parent (if that makes sense!).

So essentially I want to remove a term and all it’s children from a custom taxonomy. I tried doing several loops to loops through each subterm but with so many levels and nested loops it got a bit mind boggling!

If anyone has done anything like this before (perhaps for CustomPress?) that would be great.

Thanks!

Jack

  • Timothy
    • Chief Pigeon

    Hey there.

    Just so I understand, when talking about multiple taxonomies we are talking about top level and its child like a custom taxonomy for dates and books, then remove all categories and their children.

    Or 1 taxonomy with multiple categories and children, then removing a single cat with all of its children?

    With you mentioning wp_delete_term I presume it was the latter but just wanted to be sure.

    You could use:

    http://codex.wordpress.org/Function_Reference/get_categories

    To get the IDs:

    $category->term_id

    You could then take those IDs and run it through a loop removing all children with wp_delete_term.

    I’ve not done it, so you would need to code this up and test. Or is this something you have already tried?

    Let me know.

  • Creative Blogs
    • Site Builder, Child of Zeus

    Hi Timothy,

    You’re right it’s the latter, I have one taxonomy with multiple categories & multiple children. I want to remove one of these categories and all of it’s children.

    I’ve tried using get_terms to get the child terms and then looping through them. However with several child terms and their children it got quite complicated.

    I’ll post my code and see if I just made a simple error.

    Thanks

    Jack

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.