I have a post type location
with 2 taxonomies in it - service
& sector
.
Now, normally wordpress would show the permalink like - site_url/location/post_name
I've made it work to show -
site_url/service_slug/sector_slug/post_name
This is exactly how I want it and I've achieved it. Problem is, it broke all the page url's! Example: I have contact page which is siteurl/contact
. Now it's showing 404 when I go to the contact page url. Meaning the permalink structure works ok with my location
post type but it broke all my page permalinks.
I've updated wordpress permalink structure to /%postname%/
several times.
Here's my code below -
Location post type rewrite slug -
'rewrite' = array(
'slug' => '%sector%/%service%',
'with_front' => true
);
*I've tried 'with_front' false too, same thing.
Custom function to update slug -
function rewrite_location_post_slug( $post_link, $id = 0 ){
$post = get_post($id);
if ($post->post_type == 'location'){
$terms_service = wp_get_object_terms( $post->ID, 'service' );
$terms_sector = wp_get_object_terms( $post->ID, 'sector' );
if( $terms_service && $terms_sector){
$sector_replaced = str_replace( '%sector%' , $terms_sector[0]->slug , $post_link );
return str_replace( '%service%' , $terms_service[0]->slug , $sector_replaced );
}
} else {
return $post_link;
}
}
add_filter( 'post_type_link', 'rewrite_location_post_slug', 1, 3 );
Taxonomies don't have any rewrite slug in their code. Full code here - https://pastebin.com/3eLZhga2
Is this a wordpress bug or am I missing something? Please advise.
service-repair
. Did you get it? Would it break your strategy? The thing here is we need a standard to correctly create the pattern for the rewrite rule. The way you want it you have no standard, like a "fake pattern", which would look like:([^/]+)/([^/]+)/([^/]+)/?
. It could work, I'd have to test, but it could also break things without you knowing it.