I knew that it’s not possible to have same terms (term slugs) under categories and tags, but surprisingly, as I realized today, that’s the case with custom taxonomies as well.
So for instance, if my site has a category called Linux (linux)
and I want to create a custom taxonomy term called ‘Linux’ as well, it’d look something like Linux (linux-2)
, i.e. the slugs have to be different.
And if I try to rename the slug back to linux
, I get the error, “The slug ‘linux’ is already in use by another term”.
Is it possible to have the same terms under categories/tags and custom taxonomies (without wreaking havoc)?
How to replicate the issue?
-
Create 3-5 new posts. Create a new category and tag and assign them to those posts (just for testing purposes).
-
Save permalink settings.
-
create two new terms under the custom taxonomy with the same name and slug as the category and tag you created earlier, and assign them to a new post under a custom post type.
-
Delete the custom taxonomies.
-
Try recreating them, and you’ll now get the error: “The slug ‘XXX’ is already in use by another term.”
Sometimes that’d replicate the issue, sometimes it doesn’t.
But…
If you are able to create a category (or tag) and a custom taxonomy with the same name and slug, do it. Then, rename one of them, and you’ll notice that both the category (or tag) and the custom taxonomy are renamed!
1
Turns out, this is a bug in the core. I was about to report it, but found out that it’s been there for, like years, and applies to custom taxonomies as well. See tickets: #5809, #21950 and #22023.
If all goes as planned, it’s set to be fixed in 3.8 3.9 4.1 4.2. UPDATE: Yep, fixed!
In the meantime, here’s a plan to overcome the issue — automatically set a custom slug suffix for all newly created terms under a taxonomy:
/*
* Set custom slug suffix for terms of a taxonomy
*
* http://wordpress.stackexchange.com/q/42550/10691
* http://wordpress.stackexchange.com/q/71304/10691
* http://wordpress.stackexchange.com/q/120096/10691
* https://github.com/WordPress/WordPress/blob/master/wp-includes/taxonomy.php
*/
add_action( 'created_term', 'aahank_add_suffix_to_term', 10, 3 );
function aahank_add_suffix_to_term( $term_id, $tt_id, $taxonomy ) {
if( $taxonomy == 'book' ) {
// e.g. Term name 'PHP' and term slug 'php-books'
$term = get_term( $term_id, $taxonomy );
$args = array( 'slug' => $term->slug . '-books' );
wp_update_term( $term_id, $taxonomy, $args );
}
}
This isn’t retrospective, i.e. slugs of only the new terms under the taxonomy (‘books’ in our case) are created with our custom suffix (‘-books’).
To set a prefix instead, change this line in the function:
$args = array( 'slug' => $term->slug . '-books' );
to something like this:
// e.g. Term name 'PHP' and term slug 'books-php'
$args = array( 'slug' => 'books-' . $term->slug );
And once the bug is fixed…
Dump the database and do a regex search and replace using a proper text editor like Sublime Text or TextMate (or like this).
Probably not the best way to do it, but good enough to get the job done.