Your code should (and does from quick check) work in principle. You should not be flushing permalink every page load, just once.
Use rewrite helper like Monkeyman Rewrite Analyzer to see if rules are being generated, what they are, and how are they matching to real URLs in your site.
Related Posts:
- Prettified page URL w/ query var redirects to prettified page URL w/o query var
- why is are these rewrite_tags and rules not working?
- Redirect taxonomy to custom template to list terms in taxonomy
- Multiple values in a rewrite rule, is it possible?
- What add_rewrite_tag()’s RegEx matches against?
- Why isn’t my rewrite rule working when there is no second parameter?
- Passing variables in the permalink structure on a custom post type
- When should add_rewrite_tag() be used?
- add_rewrite_rule() vs $wp_rewrite->rules vs ‘rewrite_rules_array’?
- How to check if a rewrite rule exists
- Custom post types and custom variables — add_rewrite_tag() question
- add_rewrite_rule not producing anything in $_GET
- WordPress Custom URL Rewrites
- Case-insensitive add_rewrite_rules in WordPress functions
- Does add_rewrite_rule(..) only accept “index.php” as redirect target?
- Cyrillic characters in rewrite rules cause 404 Not Found errors
- Custom Endpoint Gives 404 Header
- Clean URLs for custom $_GET variables
- flush_rewrite_rules() cancels the effect of add_rewrite_rule()
- Separate posts by chapter per rewrite
- resolve /author/ to a page or archive (of all authors) template
- Permalinks, Rewrites, Get Variables, Oh My!
- add_rewrite_rule parameter is not received by the page
- add_rewrite_rules prefix everything
- Can someone explain the function of the third parameter of “add_rewrite_tag”
- get_query_var with add_rewrite_rule and add_filter(‘query_vars’) not working
- Reading Settings -> Front Page displays “Static Page” being reset
- add_rewrite_rule query_var not being set
- why does add_rewrite_rule refresh and loose url variables
- Custom query_vars and parse_request on wp-admin
- Associate the “add_rewrite_endpoint” and “$_GET”
- Custom Rewrite rule to match anything not already matched by WordPress
- Add Rewrite Endpoint to CPT Archive
- Optional all capture groups in rewrite rule
- Rewrite rule : custom post type with 2 numeric variiables
- Does it still make sense using json endpoint ep_mask now that there’s the new rest api? [closed]
- Simple page URL rewrite with add_rewrite_rule()
- Optimal code for two add_rewrite_rule’s
- Custom URL rewrites work, but break my permalinks
- add_rewrite_rule – Additional subpages for author pages
- Rewrite rule problem
- How do I flush rewrite rules
- Pass query vars to front page
- rewrite url in wordpress
- Enpoint Not Found in Rewrite Tag Permalink
- Rewrite rule for path of favicon.ico ( Add_rewrite_rule function possible ? )
- FLUSH_REWRITE_RULES – after or before REGISTER_POST_TYPE?
- Using get_query_var() from a plugin
- How to flush rewrite rules on the activation of any plugin
- $wp_rewrite->rules is always NULL
- Rewrite without query parameter
- WordPress CPT slug and page slug conflicts
- Mod_rewritte – remove ?m=1 from url
- Custom Post Type posts, conflict with posts after add_rewrite_rule to top
- Combine multiple CPT names to create valid permalinks
- Rewrite Rule for Post Meta
- WooCommerce – Conditional for page created by rewrite_rule
- custom permalink for single post category
- Rewrite rules priority
- Rewrite rules ignored
- add_rewrite_rule issues
- Add_rewrite_rule doesn’t seem to work?
- Grab last part of the url inside rule
- WordPress Rewrite rule is not working
- Leverage WP_Rewrite to pre-validate links / detect invalid links
- When request has multiple matching rewrite rules, and matched rule returns 404 – iterate to next rewrite rule
- How to change an existing wordpress page rewrite rule?
- “/” URLs without trailing slash shows 404
- CPT – Nice url with add_rewrite_rule
- add_rewrite_rule not working for blog category page
- get_author_posts_url() doesn’t return the author URL, because of wrong author_structure
- add_rewrite_rule only works when flush first
- Homepage rewrite rule
- custom rewrite rules will not be added to wordpress rewrite_rules database
- rewrite rules problem with #comments-2345
- Overcoming a complex WordPress page rewrite rules request
- Why isn’t my rewrite rule for add_rewrite_rule not working as expected?
- Custom Fields Query Vars and Rewrite not working
- Create new URL structure for custom post types
- A part of regular expression in add_rewrite_rule function not working
- a way to support totally different url structure
- how to use mutliple add_rewrite_rule?
- Social sharing conflict with Rewrite rules
- Last query_var not working with rewritten URL
- Rewire Rules Front Page
- How to use endpoint, but remove/rewrite endpoint base?
- WordPress URL rewrite after changing web from Joomla
- add_rewrite_rule doesn’t work for me
- Rewritten URL not loading WordPress environment
- Changing default slug of post to the post id
- Custom Rewrite Tag Structure
- Rewriting base name Taxonomy same Post Type
- Where to hook to bypass instantiating WP_Query?
- Rewrite rule help for gallery plugin
- How to Add Rewrite Ruled Argument Into Permalink Properly
- Overwrite WP’s default search rule to redirect to my own template
- Custom URL rewrite with add_rewrite_rule
- Rewrite rule to index.php in theme subfolder
- wordpress category rewrite rule with pagination
- make a new structure tag that does some processing