• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Programming / Broken Custom Post Type Permalinks

Broken Custom Post Type Permalinks

Last updated on July 15, 2016 by Sal Ferrarello

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.

Photo Credit

Pixabay

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Programming Tagged With: CPT, WordPress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in