WordPress delete_term is not working properly with add_action()?

delete_term is not working, I’m trying to delete term and after I deleted it, it’ll also delete the similar term from different a taxonomy based on their slugs

here’s my code of the delete_term, so in the code below, when I deleted a term from movies_category taxonomy, it’ll look/find for a similar slug from category (the default wordpress taxonomy for Posts), and then delete it also programmatically, but the code doesn’t work on my end, I’m not sure where did I go wrong here.

Read More
function delete_similar_term($term_id, $tt_id, $taxonomy, $deleted_term) {
    if( $deleted_term->taxonomy === 'movies_category' ) {
        $post_cat_term = get_term_by( 'slug', $deleted_term->slug, 'category' );
        wp_delete_term( $post_cat_term->term_id, 'category' );
    }
}
add_action( 'delete_term', 'delete_similar_term' , 10, 3 );

It’s hard for me to debug this also because I can’t see any errors, when I delete a term from my movies_category it will just highlight the term red and AJAX delete stops working.

Related posts

Leave a Reply

1 comment

  1. slug is unique. So $post_cat_term = get_term_by( 'slug', $deleted_term->slug, 'category' ); shouldn’t return anything.

    Instead, you may try name.

    Also, delete_term callback gets 5 parameters, so it’s better to adjust that accordingly.

    See if the following works:

    function delete_similar_term( $term_id, $tt_id, $taxonomy, $deleted_term, $object_ids ) {
        if( $deleted_term->taxonomy === 'movies_category' ) {
            $post_cat_term = get_term_by( 'name', $deleted_term->name, 'category' );
            wp_delete_term( $post_cat_term->term_id, 'category' );
        }
    }
    add_action( 'delete_term', 'delete_similar_term' , 10, 5 );