I have watched some tutorials of creating custom post type. Some tutorials says when activating / deactivating the plugin you will have to call flush_rewrite_rules. But some tutorials did not mention it at all.

So is flush_rewrite_rules necessary when creating custom post type?

I have read the codex: https://codex.wordpress.org/Function_Reference/flush_rewrite_rules

it just says that it's an expensive call, but what will happen if you don't call the flush_rewrite_rules? I have backed up my .htaccess, after calling flush_rewrite_rules (where hard is set to true by default) the old .htaccess and new .htaccess are exactly the same.

  • flush rewrite rules also recreate the rewrite rules cache. then if you have a public custom type, you need to regenerate this cache in order to access to these public posts. – mmm Jun 20 '18 at 14:50

So is flush_rewrite_rules necessary when creating custom post type?

It's not necessary, but it's a good idea. Users can flush the rewrite rules themselves just by going to Settings > Permalinks, but if the user doesn't do that and you don't flush the rules yourself then if the user tries to view a custom post they will get a 404 error.

Either way the .htaccess file doesn't change. All the WordPress .htaccess does is send all requests to index.php, which runs WordPress. WordPress then figures out what content to load by comparing the requested URL against its own rewrite rules, which are stored in the database.

For a custom post type to work a rule needs to be created that tells WordPress what the URL for a custom post type looks like. Since these rules are expensive to generate they are generated once and stored in the database. So at least once you need to generate the rules while the plugin is activated so that the custom post type rule is included. This can be done programatically with flush_rewrite_rules on activation or by the user visiting Settings > Permalinks. Doing it yourself provides a much better experience for users of your plugin.

You would also flush them on deactivation so that URLs for the post type that no longer exists properly return a 404 rather than make WordPress attempt to load a post from a non-existant post type.


It is necessary to flush the rewrite rules for WordPress to generate and cache the rewrite rules that makes posts from your post type accessible.

Until this function is called, all permalinks pointing to your newly registered custom post type will return a 404 error.

Arguably, the end user could also manually go to Settings > Permalinks from wp-admin and click "save changes" which will produce the same result.

From the user's perspective, having the plugin doing it on its own by running flush_rewrite_rules() on activation is much more convenient!

It is indeed an expensive call, but this doesn't mean you should not do it at all. This means that it should only be made when necessary - and that is on your plugin's activation and deactivation hooks - as opposed to all the time.

You absolutely do NOT want to run flush_rewrite_rules() from a function that is hooked to 'init' for example.

  • 1
    +1, but not all plugins can be activated, the most common example is a multisite setup, but there are other ways to enable plugins "by force", and you are likely to need to handle the case of adding CPTs in an upgrade of a plugin when no activation happens – Mark Kaplun Jun 20 '18 at 15:31
  • Good point, in such case one could use another way to run this function once (setting a transient once it is done and a conditional to not run it again if the transient is set would be a possibility) – Iceable Jun 20 '18 at 18:44

It is necessary just once for new post type permalinks. It's better use when plugin activate and deactivate

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.