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?
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?
Share Improve this question edited May 3, 2019 at 7:40 jnaklaas asked May 3, 2019 at 7:29 jnaklaasjnaklaas 1617 bronze badges 3 |1 Answer
Reset to default 0Problem with rules occurs, because rule for a given projecttype
is added when you visit relevant URL.
Every time you visit one of projects/{projectstype}/
addresses and flush rewrite rules,
they are deleted and only one rule (only for current project type) is saved/created.
You can add your code to request
filter hook, then there is no need to add rewrite rules.
All query vars, that are set by rules (you set only page_id
), you can set manually.
add_filter( 'request' , 'se336968_request' );
function se336968_request( $query_vars )
{
if(substr( $_SERVER['REQUEST_URI'], 0, 11 ) === '/projects/'){
$request_subpage = str_replace('/', '', str_replace('/projects/', '', $_SERVER['REQUEST_URI']));
if($request_subpage !== '') {
$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 ) {
// your current rewrite
$query_vars['page_id'] = 8;
$query_vars['post_type'] = 'page';
// --- to open "projecttype" type post from URL ---
// $query_vars['post_type'] = 'projecttype';
// $query_vars['projecttype'] = $request_subpage;
// unset( $query_vars['page_id'], $query_vars['p'], $query_vars['pagename'], $query_vars['name'] );
break;
}
}
}
}
return $query_vars;
}
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? – nmr Commented May 4, 2019 at 6:32