Make WordPress Core

Opened 8 years ago

Last modified 5 years ago

#37975 new defect (bug)

Duplicate tag names aren't supported

Reported by: ivankk's profile ivankk Owned by:
Milestone: Future Release Priority: normal
Severity: normal Version:
Component: Taxonomy Keywords: needs-patch
Focuses: administration Cc:


WP has an inherent requirement on unique tag names, if there are multiple tags with the same name, there is no way to pick which tag to assign.

Repro steps:
Create three tags
name = test, slug = test-1
name = test, slug = test-2
name = test, slug = test-3

Open the post editor and try and search for test, you will have three tags with the name test, if you try and add more than 1, you will not be able to pick which of the three you're assigning.

The search: /wp-admin/admin-ajax.php?action=ajax-tag-search&tax=post_tag&q=test
will only return tag names, and those are used to assign the tag to a post.

wp_insert_posts() calls:

if ( isset( $postarr['tags_input'] ) && is_object_in_taxonomy( $post_type, 'post_tag' ) ) {
  wp_set_post_tags( $post_ID, $postarr['tags_input'] );

Attachments (1)

37975.php (2.4 KB) - added by ivankk 8 years ago.
work around code

Download all attachments as: .zip

Change History (6)

#1 @swissspidy
8 years ago

  • Version trunk deleted

#2 @boonebgorges
8 years ago

@ivankk Thanks for the ticket, and welcome to WordPress Trac!

It seems to me that this is a problem with the tag selection interface. The underlying API allows you to add/remove terms based on term slug or ID. It's only when using the tag selector on post.php that you're unable to distinguish between duplicate tags.

One possible way forward is to show the tag slug in cases where there are duplicates. So, if you enter 'test' in the scenario described here, you'll see:

test (test-1)
test (test-2)
test (test-3)

This would require changing the behavior that prevents the selection of more than one tag, which may require changing the way that the post submission is handled.

I fear that this suggestion is not very user-friendly, though. Knowing that one tag has slug test-2 and another test-3 doesn't provide any useful information about how to select between them.

I'm surprised this has never come up before, but I can't find any related tickets. @SergeyBiryukov do any existing tickets come to mind? My Google skills are failing me.

#3 @ivankk
8 years ago

@boonebgorges thanks ;)

Focusing on just tag slug name alone won't work, as there could be scenarios where your differentiating attribute is something else, e.g. description or some custom term meta.

I've linked to a gist we used for solving this problem for the description use case. It uses get_terms + get_terms_args filters to modify the value of the select2v4 dropdown in the ajax-tag-search action with a hidden ID in the select2v4 field, since all html is rendered anyway.

Combined with a pre_post_tax_input filter, we can use the ID for setting the tags for a post.

I added a workaround for our use-case in attachment:37975.php

With the above in mind, the generalised solution is that we need to:

  1. Create a filter to add arbitrary term data (description, slug, meta etc) to the ajax action /admin-ajax.php?action=ajax-tag-search&tax=post_tag It should also always return the ID but not display it.
  2. Change the behaviour of the tag dropdown to contain a tag ID but not display it.
  3. Create a filter to change the behaviour for the tagchecklist, after a tag is added (so that we can customise what is displayed there, e.g. ID:Tag Name, Tag Name-Slug...
  4. Change the post_tag save behaviour to always expect a tag ID and always save based on tag ID.
Last edited 8 years ago by ivankk (previous) (diff)

8 years ago

work around code

#4 @swissspidy
8 years ago

Focusing on just tag slug name alone won't work, as there could be scenarios where your differentiating attribute is something else, e.g. description or some custom term meta.

Two terms with same taxonomy can't have the same slug.

#5 @desrosj
5 years ago

  • Keywords needs-patch added
  • Milestone changed from Awaiting Review to Future Release

Worth noting, this is still happening in the new block editor's tag interface.

Note: See TracTickets for help on using tickets.