Hoping you can help with this...
WE have a 'serivces' custom post type that allows a 'service' to have a parent 'service'. Our custom rewrite rule works for top-level services but not child services.
The url structure would be something like the following where 'test-service' and 'test-service-child' are the post names:
- consultancy/test-service
- consultancy/test-service/test-service-child
Our existing rewrite rule is as follows:
add_rewrite_rule('^consultancy/(.*)/?', 'index.php?post_type=service&name=$matches[1]', 'top');
Can anyone suggest a modification that will get the post name from the end of the url? Eg in the case of the examples above it should match the following
- test-service
- test-service-child
Any help greatly appreciated. Thanks in advance,
Will
Hoping you can help with this...
WE have a 'serivces' custom post type that allows a 'service' to have a parent 'service'. Our custom rewrite rule works for top-level services but not child services.
The url structure would be something like the following where 'test-service' and 'test-service-child' are the post names:
- consultancy/test-service
- consultancy/test-service/test-service-child
Our existing rewrite rule is as follows:
add_rewrite_rule('^consultancy/(.*)/?', 'index.php?post_type=service&name=$matches[1]', 'top');
Can anyone suggest a modification that will get the post name from the end of the url? Eg in the case of the examples above it should match the following
- test-service
- test-service-child
Any help greatly appreciated. Thanks in advance,
Will
Share Improve this question asked Jun 18, 2020 at 10:52 Will FairhurstWill Fairhurst 134 bronze badges1 Answer
Reset to default 0Our custom rewrite rule works for top-level services but not child services.
Your rewrite rule, or the query (the second parameter for add_rewrite_rule()
) is just missing a service
query:
add_rewrite_rule( // wrapped for brevity
'^consultancy/(.*)/?',
// Here, add the &service=$matches[1]
'index.php?post_type=service&name=$matches[1]&service=$matches[1]',
'top'
);
But, why don't you just use consultancy
as the rewrite slug
when you register the post type?
register_post_type( 'service', [
'public' => true,
'rewrite' => [ 'slug' => 'consultancy' ],
'hierarchical' => true,
// .. other args here.
] );
That way, you wouldn't need the custom rewrite rule because the above 'slug' => 'consultancy'
will set the permalink structure for your "service" posts to consultancy/<post name/slug>
.
Can anyone suggest a modification that will get the post name from the end of the URL?
You can use:
get_query_var( 'service' )
orget_query_var( 'name' )
to get the complete post name/slug path from the current URL.So if the URL is
https://example/consultancy/test-service/test-service-child
, you'd gettest-service/test-service-child
.get_queried_object()->post_name
to get the actual post name/slug for the queried post, i.e. without the parent path.So if the URL is
https://example/consultancy/test-service/test-service-child
,get_queried_object()->post_name
would returntest-service-child
(note thechild
).