I have 2 custom post types
Offices
register_post_type('offices',
[
'labels' => [
'name' => __( 'Offices' ),
'singular_name' => __( 'Office' )
],
'description' => 'Our Office.',
'public' => true,
'hierarchical' => true,
'has_archive' => false,
'menu_icon' => 'dashicons-building',
'support' => ['title', 'custom_fields', 'page-attributes']
]
);
Office members
//Team Members
register_post_type( 'office_members',
[
'labels' => [
'name' => __( 'Team Members' ),
'singular_name' => __( 'Team Member' )
],
'description' => 'Team members for offices.',
'public' => true,
'hierarchical' => false,
'has_archive' => 'offices/([^/]+)/members',
'show_in_menu' => 'edit.php?post_type=offices',
'support' => ['title', 'custom_fields', 'page-attributes']
]
);
I want the following urls to work
example.com/offices
- Shows the office archive page
example.com/offices/([^/]+)
- Show the single office page
example.com/offices/([^/]+)/members
- Show the members archive page where parent is the office
example.com/offices/([^/]+)/members/([^/]+)
- Show the single member page
I have the following rewrite rules for the office members
add_permastruct('office_members', '/offices/%office%/members/%office_members%', false, ['walk_dirs' => false]);
add_rewrite_tag('%office_members%', '([^/]+)', 'office_members=');
add_rewrite_rule('^offices/([^/]+)/members/([^/]+)?','index.php?office_members=$matches[2]','top');
All of my urls work except for the members archive page. It loads the template file archive-office_members.php
which is great, but it doesn't detect the parent office in the url. So instead of it only showing members for that office it just shows all members.
How can I set my url so that it shows the members archive page, but only shows members based on the office
in the url, and so all 4 of my urls work?