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?
- Why does everybody hook add_rewrite_rule on init
- add_rewrite_rule() vs $wp_rewrite->rules vs ‘rewrite_rules_array’?
- How to check if a rewrite rule exists
- Regex in add_rewrite_tag not accepting OR operators?
- Querystring parameter getting lost in rewrite rule
- Rewrite Rules for Multiple (more than 2) Taxonomies
- Custom post types and custom variables — add_rewrite_tag() question
- add_rewrite_rule not producing anything in $_GET
- Flush_rewrite_rules not working when settings updated
- 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
- Making extra parameters optional
- flush_rewrite_rules() cancels the effect of add_rewrite_rule()
- Separate posts by chapter per rewrite
- How to change the matches in add_rewrite_rule
- Using add_rewrite_rule() to redirect to Front Page
- resolve /author/ to a page or archive (of all authors) template
- add_rewrite_rule() not working
- Permalinks, Rewrites, Get Variables, Oh My!
- add_feed and flush_rewrite_rules
- Rewrite rules and query for virtual page
- 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”
- custom permalink/shortlink with base62 encoded post ID
- Best action to call add_rewrite_rule
- How to restore WP 5.4 behaviour where a numeric string could added to each page URL and parsed as “page” in WP 5.5?
- get_query_var with add_rewrite_rule and add_filter(‘query_vars’) not working
- Can rewrites fallback if 404? Rewrite conflict issue
- 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
- Where do I USE add_rewrite_rule?
- add_rewrite_rule confusion
- add_rewrite_rule isnt working, not getting added to rules array, why?
- 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
- Uppercase to Lowercase in URL
- How to use rewrite rule or rewrite endpoint to switch languages?
- How to add custom variable in url without redirect?
- Remove rewrite endpoint on deactivation?
- Do I need to flush rewrite rules when creating new user if using custom author rewrite rules?
- 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()
- Hooking “delete_option_rewrite_rules” for rewrite rule addition
- Optimal code for two add_rewrite_rule’s
- Custom URL rewrites work, but break my permalinks
- WordPress shows front page, when it should show 404 with pagination style urls
- add_rewrite_rule – Additional subpages for author pages
- Can’t get custom rewrite tag, query var, permastruct (permalink structure), and rewrite rule to work properly together
- Rewrite URLs – Custom Post Type – Post Slug, Taxonamy Slug
- Rewrite rule problem
- How do I flush rewrite rules
- add_rewrite_rule redirects if value = 1
- Pass query vars to front page
- rewrite url in wordpress
- Custom permalink leads to index page
- add_rewrite_rule permalinks break in WordPress 4.5
- How to add rewrite rule for external php file
- Help with rewrite rules for two post types
- Enpoint Not Found in Rewrite Tag Permalink
- Making extra parameters optional
- My add_rewrite_rule is returning a 404
- Rewrite rule for path of favicon.ico ( Add_rewrite_rule function possible ? )
- wordpress add_rewrite_rule not working
- 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
- force_ssl_admin() causing issues with preview links
- How to test custom rewrite rules /permalinks?
- $wp_rewrite->rules is always NULL
- Rewrite without query parameter
- WordPress CPT slug and page slug conflicts
- Mod_rewritte – remove ?m=1 from url
- Rewrite Rules problem when rule includes homepage slug
- Get wordpress installation folder
- Custom Post Type posts, conflict with posts after add_rewrite_rule to top
- help with custom rewrite rules
- Order of rewrite rules when calling add_rewrite_rule
- Combine multiple CPT names to create valid permalinks
- Rewrite Rule for Post Meta
- WooCommerce – Conditional for page created by rewrite_rule
- add_rewrite_rule() not stored (I think)
- custom permalink for single post category