8

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?

  1. Create 3-5 new posts. Create a new category and tag and assign them to those posts (just for testing purposes).

  2. Save permalink settings.

  3. 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.

  4. Delete the custom taxonomies.

  5. 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!

  • Hmm, this doesn't happen for me. Can you paste your custom taxonomy code? – Matthew Boynes Oct 25 '13 at 17:24
  • @MatthewBoynes Oh, this is what it looks like: gist.github.com/anonymous/5267905cccae4d52cc1b – its_me Oct 25 '13 at 17:38
  • I'm using the latest nightly build of WordPress and your code, verbatim, does not encounter this error. Here's what you should do next: (1) In a clean install of WordPress 3.6.1, add just your post type and taxonomy code to a clean theme. Use Twenty Thirteen or Toolbox or something. If it works, you know the issue isn't in WP but instead with a plugin or your theme. If it does occur, try the same thing with the latest nightly build of 3.7. If it works, then you don't have to worry because 3.7 launches probably next week. If it happens still, provide all code necessary to replicate it. – Matthew Boynes Oct 25 '13 at 17:57
  • @MatthewBoynes Okay. I'll test it out and see. By the way, I'm already on 3.7 stable, released sometime yesterday/today. – its_me Oct 25 '13 at 18:01
  • Ah, you're right, I somehow missed that! – Matthew Boynes Oct 25 '13 at 18:05
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.