I'm honestly getting pretty lost, and could use some guidance to wrap up a project I've been working on
I have a single hierarchical taxonomy, county that I'm using on two of my custom post types, floorplan and ready-made.
I am trying to set up permalinks to the custom taxonomy pages that will display as:
{post-type}/{parent-tax}/{child-tax}
Right now, you can navigate to either or, so floorplans/{parent-tax} and floorplans/{child-tax} will lead to the same county-taxonomy.php template. You can do the same for ready-made/{parent-tax} and ready-made/{child-tax}.
However, if you are going directly through the taxonomy, and you navigate to the child taxonomy, the url will display as county/{parent-tax}/{child-tax}.
What I can't figure out, is how to append the parent taxonomy to the child-taxonomy permalink when you're navigating by the custom post type base. floorplans/{parent-tax}/{child-tax} and ready-made/{parent-tax}/{child-tax} is a 404.
Do I need to write a rewrite rule? I'm kind of at a loss on how to do that. I followed along heavily with this guide in order to get my parent and child taxonomies displaying in the post type permalinks themselves, but the final piece of the puzzle is to be able to navigate to the child taxonomy pages specific to each post type, and include the parent taxonomy in that url.
My rewrite rules as of now look almost identical to the guide I linked,
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules)
{
$newRules = array();
$newRules['floorplan/(.+)/(.+)/?$'] = 'index.php?floorplan=$matches[2]'; // my custom structure will always have the post name as the 5th uri segment
$newRules['ready-made/(.+)/(.+)/?$'] = 'index.php?ready_made=$matches[2]'; // my custom structure will always have the post name as the 5th uri segment
$newRules['floorplan/(.+)/(.+)/'] = 'index.php?county=$matches[1]'; // my custom structure will always have the post name as the 5th uri segment
$newRules['ready-made/(.+)/(.+)/'] = 'index.php?county=$matches[1]'; // my custom structure will always have the post name as the 5th uri segment
$newRules['floorplan/(.+)/?$'] = 'index.php?county=$matches[1]';
$newRules['ready-made/(.+)/?$'] = 'index.php?county=$matches[1]';
return array_merge($newRules, $rules);
}
I added those two middle values thinking that that would target the permalink structure for parent-tax/child-tax with nothing trailing behind it, but I'm not sure how to piece it together from there. Any tips would be hugely appreciated!
to answer my own question,
here is the final rewrite function in order to achieve the desired results:
add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules)
{
$newRules = array();
$newRules['floorplan/(.+)/(.+)/(.+)/?$'] = 'index.php?floorplan=$matches[3]';
$newRules['ready-made/(.+)/(.+)/(.+)/?$'] = 'index.php?ready_made=$matches[3]';
$newRules['floorplan/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';
$newRules['ready-made/(.+)/(.+)/?$'] = 'index.php?county=$matches[2]';
$newRules['floorplan/(.+)/?$'] = 'index.php?county=$matches[1]';
$newRules['ready-made/(.+)/?$'] = 'index.php?county=$matches[1]';
return array_merge($newRules, $rules);
}
I have kind of cobbled this together from other stackoverflow answers, but as far as I can tell, these rewrites are essentially saying:
for the given url, display the post-type/taxonomy-type/etc where matches[x]
is the actual page title of the thing you're trying to display.