I have the following code in my functions.php file. I have a page projects. I have two custom post types: project and projecttype. An individual project post has it's own permalink /projects/project-name. Projecttypes do need to show the 'projects' page.
My code checks if the url is a subpage of /projects/. If the subpage is a projecttype, it loads the template for the projects page (id 8).
if(substr( $_SERVER['REQUEST_URI'], 0, 11 ) === '/projects/'){
$request_subpage = str_replace('/', '', str_replace('/projects/', '', $_SERVER['REQUEST_URI']));
if($request_subpage !== '') {
$valid_subpage_url = false;
$project_types = get_posts( array( 'post_type' => 'projecttype', 'posts_per_page' => -1 ) );
foreach( $project_types as $project_type ) :
if( $project_type->post_name === $request_subpage ) :
$valid_subpage_url = true;
break;
endif;
endforeach;
//flush_rewrite_rules();
if($valid_subpage_url) add_rewrite_rule( "^projects\/$request_subpage$", "index.php?page_id=8", 'top');
}
}
When I uncomment flush_rewrite_rules();
my code works. When I don't flush the rewrite rules, it does not work (all projecttype subpages return a 404). Since flush is an expensive operation, I probably shouldn't use it.
What's going on here?
projecttype
posts that load the same page? As noted, the name suggests thatprojecttype
could be taxonomy. What doesprojects
page do? Depending on theprojecttype
displays different content?