Unable to delete a Category and Tag that share same slug

In my wordpress site I have a category (e.g Animals) with slug (anim) and a tag with name (Animals) with the same slug (anim).

My problem is that I want to delete the category but keep the tag. However whenever I try to change any settings in the category, the tag changes as well (e.g when I change the category slug to anim-old the tag’s slug changes as well).

Read More

It seems to be something to do with the fact that they have the same slug wordpress thinks they’re the same.

Would there be any way (even in the database using sql) to unlink the category and tag so that I can delete the category?

Related posts

1 comment

  1. The problem is WordPress database schema for taxonomies. In database, two terms in 2 different taxonomies with same slug share the same row on terms table. They are differentiate with 2 rows on the terms_taxonomy table.

    So if you want to delete only the category, you have to perform a custom sql on WordPress database.

    I wrote a function for the pourpose:

    function delete_slug_sharing_term( $slug = '', $taxonomy = 'category' ) {
      $term = get_term_by( 'slug', sanitize_title($slug), $taxonomy );
      if ( empty($term) || is_wp_error($term) ) return FALSE;
      global $wpdb;
      $q = "DELETE FROM $wpdb->term_taxonomy 
           WHERE taxonomy = %s 
           AND term_taxonomy_id = %d";
      return (bool) $wpdb->query( $wpdb->prepare( $q, $taxonomy, $term->term_taxonomy_id) );
    }
    

    Use it like so:

    delete_slug_sharing_term( 'animals', 'category' );
    

    you need to call it only once, so put somewhere (maybe on ‘admin_init’), go on your dashboard, check the category is removed, and if so, delete the function call (and the function too, if you want).

Comments are closed.