Post types are registered on every load. They’re not stored in the database or some other sort of persistent storage. The posts are, but not the post types.
So the correct place to flush rewrite rules is when the theme is activated. This can be done with the after_switch_theme hook:
add_action( 'after_switch_theme', 'flush_rewrite_rules' );
For a plugin, you’d do it with the activation and deactivation hooks:
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'flush_rewrite_rules' );
Related Posts:
- Mixing custom post type and taxonomy rewrite structures?
- WordPress Rewrite Rules for Custom Post Type and Taxonomy
- How to pass URL parameters for advanced taxonomy queries with multiple terms for one custom taxonomy
- Custom Post Type and Taxonomy URL rewrite
- How can I rewrite URLs to pass taxonomy and post type values to the query?
- How to modify URL structures in custom post types and taxonomies or terms
- Archive page with multiple taxonomies rewrite
- WordPress custom taxonomy URL rewrite on spelling errors
- Taxonomy archives based on Custom Post Type
- Multiple custom post types using the same taxonomy = URL frustrations
- Permalink rewrite with custom post type and custom taxonomy
- Display CPT taxonomies as an archive page
- Including two taxonomies in a permalink structure
- WP Rewrite Rules – Custom post type & taxonomy
- Many to Many Taxonomies or rewrite rules?
- Remove base slug in CPT & CT, use CT in permalink
- How to rewrite custom post type URL for multiple depths instead of one specific depth
- How do I list terms of a custom taxonomy at i.e. domain.com/brands/
- Permalink Structure problem with cpt and custom taxonomy
- Custom Taxonomy 404
- Bulk 301 redirect for custom post type
- Category base to url in custom post type/taxonomy
- Is slug “type” reserved and can’t be used in taxonomy rewrite?
- Custom Permalink Tag breaks Pagination
- Custom Permalink for Taxonimie and Custom Post Type
- Permalinks when filtering multiple custom post types by single taxonomy
- custom taxonomy archive by year
- Custom Post Type and Custom Taxonomy Permalinks
- How can you handle rewrite rules for custom post types with option to filter on custom taxonomy terms?
- Permalink Structure CPT With Custom Taxonomy Fails
- Conditionally custom post type url rewrite
- Change Permalink Structure for Tag.php template
- Custom taxonomy in URL showing 404
- Custom Taxonomy URL are redirecting to page with the same name
- Remove url rewrites for registered taxonomies
- Get Taxonomy Term Title by it’s URL
- Taxonomy custom post type URL
- How Can I Change the Custom Post Type Slug to the Taxonomy Slug in the URL?
- Use same slug base for Custom Post Type posts, and multiple taxonomy terms
- How to create custom url structure based on custom post type rewrite slug, taxonomy and category slug?
- How can I make the slug of the taxonomy also the slug of the custom post
- Customize Custom Post Type URL
- How do I change the custom taxonomy slug to be the same as the custom post type slug?
- WP Rewrite Rule Issue while using Custom post type & taxonomy
- Custom Permalinks with CPT and Hierarchical Taxonomies
- WordPress uses url different from defined in rewrite arg of custom post type
- Custom Permalink Structure for Custom Post Type, Custom Taxonomy, Under a page
- Custom taxonomy returns 404
- Including taxonomy term before post type breaks top level pages
- Correct procedure for advanced permalinks
- How to get list of taxonomy slugs ordered parents>childs?
- Taxonomy in URL
- Custom taxonomy and post type with same slug
- Custom permalink with child taxonomy terms
- Best way to specify “article type” in URL
- I can’t access custom taxonomy page listing
- post_type_link Filter – Too Few Arguments
- How to rewrite custom taxonomy term archive to use THE SAME SLUG/front as CPT
- WordPress custom permalink and archive link for custom post type
- How to rewrite the post slug of a custom post type post
- Urls in Custom Post Type work for Terms but not for its Taxonomies
- Custom Post Type View Button in Admin Gives 400 Error
- Taxonomy rewrite pagination 404
- Archive page for custom post type and custom taxonomy
- Custom Post Type URL Rewrite’s
- custom post archive URL is wrong
- Custom taxonomy and post type rewrite rules
- Custom Post Type and Taxonomy Rewrite Error
- Custom taxonomy terms as children of multiple custom post types
- Custom Permalink For Custom Post Type – Working Fine But Broken For Pagination
- Custom Post Type & Taxonomies – Rewrite
- Remove custom permalink base from CPT custom category and custom tags
- How to properly set the rewrite rules for this case?
- How to handle this wordpress custom post type rewrite problem?
- Display CPT taxonomies as an archive page
- Change CPT permalink to use the category
- Permalinks: custom post type -> custom taxonomy -> custom sub taxonomy -> post
- Redirect Custom Post Type from Child Taxonomies
- Pretty Permalinks with CTP and Taxonomies / Hierarchical
- Customise Permalink Structure For Custom Post Types
- Custom URL for Custom Post Type & Custom Taxonomy Term
- Custom taxanomy and custom post type – pagination leads to 404
- Register post type getting error 404
- I have a custom taxonomy assigned to two post types. How do I create URLs for different term archives for each post type?
- CPT url rewriting : regular posts return 404 error
- Custom Post Type rewrite url to /category/pagetitle [duplicate]
- Remove custom post type slug but keep related category taxonomy permalink
- page not found for example.com/custom-post-name
- redirect old post type url to new structure url
- One custom post type with two different url structure base on taxonomy term
- Why won’t this rewrite rule work?
- Customize category URL
- Custom Post Type Base URL
- Custom Taxonomy in Permalink from post type
- How can i change url structure of cpt like this?
- How to change custom post type slug without damage seo? [closed]
- Rewrite Rule for custom post type link with or without taxonomy terms
- Custom post type with custom taxonomies structure url not working archive of CPT
- How to work with URLs where sometimes a post or a subcategory is in the same part of the URL structure
- Rewrite taxonomy permalink appended to CPT archive url