Opens in a new tab
🚀 Your daily business tech & AI briefing — Subscribe free →

WordPress REST API: ‘Slug already exists’ — 5 fixes that work

Zain A
Share this article

Introduction

Why the REST API rejects duplicate slugs

This error usually means WordPress has a hidden or trashed item holding your slug, not that your request is wrong. Save yourself the rabbit hole: clear trash, check for collisions across post types and taxonomies, and set a unique slug in the payload before blaming the API.

Where the error appears: terms, posts and product attributes

  • Taxonomy terms
  • Posts, where WordPress usually adds a numeric suffix
  • WooCommerce product attributes
WordPress REST API: ‘Slug already exists’ — 5 fixes that work

1. Look Up the Existing Item by Slug First

Querying the endpoint with the slug parameter

Before you create anything, ask the WordPress REST API whether the slug is taken. Send a GET request to the collection endpoint with the slug parameter, for example the terms route for your custom taxonomy.

Reading the returned ID

An empty array means the slug is free, so go ahead and create the term. If a match comes back, read its term ID from the response.

  • Empty result: create the item.
  • Match found: store the ID and switch to an update.

2. Update the Existing Term Instead of Creating a New One

Sending the update request by ID

Once you have the term ID, send a POST request to the term’s own route, with the ID at the end. Pass only the fields you want to change, such as the name or description.

Handling categories, tags and custom taxonomies

Categories and tags are just rest_base values, so the same logic applies to all of them. For a custom taxonomy, use the rest_base you set when you registered it.

  • Categories and tags: use their default routes.
  • Custom taxonomy: confirm show_in_rest is enabled.

3. Generate a Unique Slug Before Sending the Request

Adding a suffix or modifier

If you need a new item, make the slug distinct yourself. Append a modifier such as a year, a source name or a counter before you send the request.

WordPress does the same for posts, adding a number like slug-2 when a title repeats.

Using the wp_unique_post_slug filter

For posts, the wp_unique_post_slug filter lets you control how a duplicate slug is rewritten on the server side. Your code decides the final value, so imports stop colliding.

WordPress REST API: ‘Slug already exists’ — 5 fixes that work

4. Remove Trashed or Stale Items That Hold the Slug

Finding drafts and trashed posts using the slug

A slug can stay reserved by an item you no longer see. Query the posts endpoint with the slug parameter and the status parameter set to include draft and trash.

Deleting permanently with the force parameter

Send a DELETE request to the item’s route with force set to true. This skips the trash and frees the slug right away.

  • Confirm the item is truly stale first.
  • Terms have no trash, so deletion is always permanent.

5. Resolve WooCommerce Product Attribute Slug Conflicts

Renaming or deleting the existing attribute

The WooCommerce product attributes controller returns an error when an attribute slug already exists. Choose one of three routes:

  • Use a different slug for the new attribute.
  • Update the existing attribute.
  • Delete the existing attribute if it is unused.

Fixing the slug-already-exists error in imports

Query the attributes endpoint first and match on slug. If it exists, update it by ID instead of creating it again.

FAQ

Why does the WordPress REST API return a slug already exists error?

The slug is already in use. Term creation fails when another term holds it.

How do you check whether a slug exists in the WordPress REST API?

Send a GET request to the collection endpoint with the slug parameter. An empty array means it is free.

Does WordPress allow duplicate post slugs?

Not for the same post type. WordPress normally adds a numeric suffix to a duplicate post slug.

Conclusion

A “slug already exists” error is a signal, not a dead end. Look the item up first, then update it, rename it, or clear what holds the slug.

It resolves most collisions in one request.

References

Share this article

Stay in the Loop

Weekly tech insights, AI news and tools — straight to your inbox.

Newsletter Form (#4)

Contents