WordPress REST API Slug Fix: Choose the Right One
In this article
- Background: How Slugs Work in the REST API
- How to Change an Individual Post’s Slug via the API
- How to Change the Post Type Archive Slug
- Why Your Slug Change Doesn’t Seem to Work
- Comparing the Two Fixes: Which Should You Use?
- Common Problems and Misconceptions
- Alternatives to the REST API for Slug Changes
- Safety and Best Practices
- Frequently Asked Questions
The correct fix for a WordPress REST API slug problem depends entirely on what you are trying to do: if you need to change the URL structure of your custom post type or taxonomy, you should use the rewrite argument in register_post_type() or register_taxonomy() (and then flush rewrite rules). If you need to change the slug of an individual post or page via the API, you update the slug field in a POST or PUT request to that specific resource. This article explains both approaches, when to use each, and the common pitfalls that cause slugs to appear unchanged.
Background: How Slugs Work in the REST API
In WordPress, a slug is the URL-friendly version of a post title (e.g., my-awesome-post). The REST API exposes this as the slug property on post, page, and custom post type objects. When you make a request to /wp-json/wp/v2/posts, each item includes a slug field. The API also uses slugs in the URL for single resources, like /wp-json/wp/v2/posts/my-awesome-post.
The confusion usually arises because there are two distinct places where slugs are defined:
- Content slugs – the slug of a specific post, page, or custom post type item. This is stored in the
post_namecolumn in the database and exposed via the API asslug. - Archive/rewrite slugs – the URL prefix for an entire post type or taxonomy archive, such as
/news/for a “news” post type. This is set in therewriteargument during registration.
Many “slug fix” tutorials conflate these two, leading to code that changes one but not the other. Knowing which one you need is the first step.
How to Change an Individual Post’s Slug via the API
To update the slug of a single post, you send a POST or PUT request to the resource endpoint. For example, to change the slug of post ID 123 to new-slug:
POST /wp-json/wp/v2/posts/123
{
"slug": "new-slug"
}If you are using JavaScript with the apiFetch package, the code looks like this:
apiFetch( {
path: '/wp/v2/posts/123',
method: 'POST',
data: { slug: 'new-slug' }
} );Important notes:
- You must authenticate (typically with a nonce for logged-in users or an application password for external clients) and have the
edit_postscapability. - The slug is sanitized automatically: spaces become dashes, uppercase becomes lowercase, and non-ASCII characters are converted to their URL-safe equivalents.
- If another post already uses that slug, WordPress will append a number (e.g.,
new-slug-2) to keep it unique. The API response will show the final slug, so check that. - After changing the slug, the old URL will return a 404 unless you set up a redirect. WordPress does not automatically redirect old slugs.
How to Change the Post Type Archive Slug
If you want to change the URL prefix for all items of a custom post type (e.g., from /news/ to /articles/), you must modify the rewrite argument in the post type registration. This is not something you can do via the REST API directly – it is a code-level change. Here is an example:
register_post_type( 'news', array(
'rewrite' => array( 'slug' => 'articles' ),
// other arguments
) );After making this change, you must flush rewrite rules. You can do this by going to Settings → Permalinks and clicking “Save Changes” (no need to change anything), or programmatically by calling flush_rewrite_rules() once (e.g., on theme activation).
If you are using a plugin like Custom Post Type UI, you can change the slug in the plugin’s settings and it will handle the rewrite flush for you.
Why Your Slug Change Doesn’t Seem to Work
There are several common reasons why a slug update via the REST API appears to fail:
- You are editing the wrong object. Make sure you are targeting the correct post ID. A quick way to verify is to GET the object first and check its current slug.
- The slug is being overwritten by a plugin or theme. Some plugins (e.g., SEO plugins) automatically set slugs based on titles. Check if any code is hooking into
wp_insert_post_dataand forcing a slug. - You forgot to flush rewrite rules. This only applies to archive slug changes, but it is the most common cause of “nothing happened” after updating a post type slug.
- You are looking at a cached version. The REST API response might be cached by a plugin or server. Try adding a query string like
?nocache=1or clearing the cache. - The slug is reserved. WordPress reserves certain slugs like
page,attachment, andrevision. Trying to use them will result in a sanitized alternative.
Comparing the Two Fixes: Which Should You Use?
To make the decision clear, here is a comparison:
- Individual post slug – Use the REST API to update the
slugfield. This is the only way to change a single item’s URL without editing the database directly. - Post type archive slug – Use the
rewriteargument in code. The REST API does not expose this setting. - Taxonomy slug – Similar to post type archive: change the
rewriteargument inregister_taxonomy()and flush rules.
If you are building a plugin or theme and need to allow users to change the archive slug from a settings page, you would store that value in an option and pass it to the rewrite argument dynamically. For example:
$slug = get_option( 'my_post_type_slug', 'news' );
register_post_type( 'news', array(
'rewrite' => array( 'slug' => $slug ),
) );Remember to flush rewrite rules when that option is saved.
Common Problems and Misconceptions
One misconception is that changing a post’s slug in the REST API will automatically redirect the old URL. It does not. You need to implement a redirect (e.g., using the template_redirect hook or a plugin) if you want to preserve SEO value.
Another issue is that some developers try to change the slug of a post type by using register_post_type() on an already-registered post type. This will not work – the post type is already registered, and the new arguments are ignored. You must change the original registration code.
Also, be aware that the REST API’s slug field is read-only for some users without edit permissions. If you get a 403 error, check your authentication method and capabilities.
Alternatives to the REST API for Slug Changes
If you are not comfortable with the REST API, there are other ways to change slugs:
- WordPress admin – Edit the post and change the “Permalink” field directly. This is the simplest for a single post.
- Bulk edit – In the posts list screen, you can use “Quick Edit” to change slugs for multiple posts at once.
- Database update – Directly update the
post_namecolumn in thewp_poststable. This is risky and not recommended unless you know what you are doing. - Plugins – Some SEO plugins offer “slug manager” features, but they often just wrap the same core functionality.
For archive slugs, there is no admin UI in core WordPress. You must use code or a custom post type plugin.
Safety and Best Practices
Before making any slug changes, consider these safety measures:
- Back up your database – If you are changing many slugs, a backup lets you revert if something goes wrong.
- Test on a staging site – Especially for archive slug changes, test on a copy of your site first.
- Set up redirects – For individual posts, use a redirect plugin or custom code to map old URLs to new ones.
- Check for hardcoded URLs – If you have hardcoded links to old slugs in your content or theme, they will break.
Also, note that changing a post type archive slug will affect all existing posts of that type. Search engines may take time to re-index the new URLs, so monitor your traffic and crawl errors.
Frequently Asked Questions
Can I change a post slug with a GET request?
No. GET requests are read-only. You must use POST (for existing resources) or PUT (for updates) with authentication. A GET request will only return the current slug.
Why does my new slug get a number appended like “-2”?
WordPress enforces unique slugs per post type. If another post already has the slug you want, it appends a numeric suffix. The API response will show the final slug, so you can read it and adjust if needed.
Does changing a slug affect the REST API endpoint URL?
Yes. The single-post endpoint changes from /wp-json/wp/v2/posts/old-slug to /wp-json/wp/v2/posts/new-slug. However, the endpoint using the numeric ID (e.g., /wp-json/wp/v2/posts/123) remains the same.
How do I flush rewrite rules after changing a post type slug?
The easiest way is to go to Settings → Permalinks and click “Save Changes” – you don’t need to modify anything. For developers, call flush_rewrite_rules() once during a plugin activation or theme setup, but not on every page load.
Can I change the slug of a built-in post type like “post” or “page”?
Yes, for individual posts you can change the slug field via the API. For the archive slug of the built-in “post” post type, you can change the base in the permalink settings (e.g., from /blog/ to /news/), but this is not a REST API feature.
What if I get a 403 error when trying to update a slug?
This means you are not authenticated or you lack the edit_posts capability. Check that you are sending a valid nonce or application password, and that the user has the appropriate role. For custom post types, the capability might be different (e.g., edit_my_cpt).