Opened 8 years ago
Last modified 6 years ago
#37975 new defect (bug)
Duplicate tag names aren't supported
Reported by: | ivankk | Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Taxonomy | Keywords: | needs-patch |
Focuses: | administration | Cc: |
Description
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'] );
}
https://core.trac.wordpress.org/browser/tags/4.6.1/src/wp-includes/post.php#L3238
Attachments (1)
Change History (6)
#3
@
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:
- 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.
- Change the behaviour of the tag dropdown to contain a tag ID but not display it.
- 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...
- Change the post_tag save behaviour to always expect a tag ID and always save based on tag ID.
@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:
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 anothertest-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.