When you first add a new WordPress Custom Post Type (a.k.a. CPT), you may find the URLs do not work.
Check Your Custom Post Type
First, check that your CPT code is being executed and you see your CPT on the backend (i.e. in /wp-admin/
).
Second, make certain your Custom Post Type is set to public. The code 'public' => true,
should appear in your call to register_post_type()
.
Flush Your Rewrite Rules
If your CPT permalinks still don’t work, you made need to clear the Permalink Rewrite Rules cache.
WordPress creates a lot of complex rules that it uses to determine what content to return for which permalink, these are called rewrite rules. Because of the complexity of these rules (and the time it takes to generate them), WordPress calculates these rules once and then stores them for future use (this is called caching).
We need to prompt WordPress to throw out the current set of rewrite rules and calculate a new set. Calculating a new set of rules will take more time and effort but once they’re recalculated WordPress will cache these new rules.
The easiest way to flush the rewrite rules is to visit the Settings > Permalinks page ( /wp-admin/options-permalink.php
). You don’t need to change anything on this page, just by visiting it, the old WordPress rewrite rules are removed and new ones are created.
Clearing the Permalink Rewrite Rules with Code
If you are creating a plugin for distribution that adds a Custom Post Type, you should automatically flush the rewrite rules and spare your user a trip to the Settings > Permalinks page.
Leave a Reply