How to remove CPT slug from custom post type URLs?

How to remove CPT slug from custom post type URLs?

This is probably because WordPress doesn’t give a streamlined way to remove CPT slug from custom post type permalinks and therefore these answers are either based on considering particular scenarios or based on a hacky way. Here is what I have come up with while trying to create a solution that works on most if not all scenarios.

Why are my CPT slugs not working in WordPress?

After solving the first part, your CPT permalinks don’t have CPT slugs anymore. But, now, the problem is WordPress doesn’t know how to find your posts from those new permalinks because all it knows is CPT permalinks have CPT slugs. So, without a CPT slug in the permalink, it can’t find your post.

Is it possible to create duplicate slugs in WordPress?

As WordPress doesn’t automatically prevent creating duplicate slugs across different post types, you may find problems accessing posts having the same post slugs because of losing uniqueness in CPT permalinks after removing the CPT slugs.

How to disable custom post type in WordPress?

Step 1: Disable rewrites on your custom post type by setting rewrites to ‘false’ when you register the post: Step 2: Manually add our custom rewrites to the bottom of the WordPress rewrites for our custom_post_type NOTE: Depending on your needs, you may want to modify the above rewrites (disable trackbacks? feeds?, etc).

Which is an example of a slug url?

To give a concrete example, assume you had a Newspaper website (such as we’ll build in this tutorial). For a story titled “Hello World,” the URL would be example.com/hello-world assuming the site was called example.com. Despite their ubiquity, slugs can be somewhat challenging to implement the first time around, at least in my experience.


Why does the custom post type rule always match?

If you look at the picture above it becomes clear why: the custom post type rule will always match against a page slug. Because WordPress has no way of identifying if it’s a page or a custom post type that does not exist, it will return 404.