I'm trying to add a 'sub post' to each post on my site, eg
site.com/product-one/changelog site.com/product-two/changelog site.com/product-three/changelog
This is the code I'm using atm, which I found here somewhere:
global $wp,$wp_rewrite;
$wp->add_query_var('sub-page');
$wp_rewrite->add_rule('([^/]+)/(changelog)','index.php?pagename=$matches[1]&sub-page=$matches[2]', 'top');
$wp_rewrite->flush_rules(false); // This should really be done in a plugin activation
This works fine if I browse to site.com/product-one?sub-page=changelog but if I goto site.com/product-one/changelog i just get redirected back to site.com/product-one
So I disabled the canonical redirect as a test using:
remove_filter('template_redirect', 'redirect_canonical');
site.com/product-one/changelog then doesn't redirect but does return a 404 and site.com/product-one?sub-page=changelog still works.
Is there something I can change with my rewrite to make this work?
Note: I know I could do this easy with custom post types and I started to go down that route, but because the site is already using the %postname% permalink structure, I then wouldn't be able to have the custom post type have that structure as well as the sites pages.