As the title states, I'd like to remove parent slugs from URLs for a particular post type: services.

Something that would change this:


To something like this:


I'd rather not use an additional plugins to accomplish this. I'm currently delivering the CPT via a plugin, which also registers a custom taxonomy.

  • Does your custom post type need to be hierarchical? If not, set that to false and the URLs won't be hierarchical – Matthew Boynes May 28 '13 at 22:35
  • yes, they do need to be hierarchical. this is mainly for archivial pages – NW Tech May 28 '13 at 22:37

In a quick test, I was surprised to find that this works out of the box. That is, the canonical URI for a child post still has the parent in the path, but the child post works just as well without it (doesn't 404, doesn't redirect). As a result, it should just be a matter of filtering post_type_link to get this to work as you're asking! The following code should do just that:

function wpse_101072_flatten_hierarchies( $post_link, $post ) {
    if ( 'service' != $post->post_type )
        return $post_link;

    $uri = '';
    foreach ( $post->ancestors as $parent ) {
        $uri = get_post( $parent )->post_name . "/" . $uri;

    return str_replace( $uri, '', $post_link );
add_filter( 'post_type_link', 'wpse_101072_flatten_hierarchies', 10, 2 );
  • Minor oversight. When I copied/pasted the code you gave, it didn't work, then I noticed the lack of the 's' on the post type. Boom! Everything is working. Thanks! – NW Tech May 29 '13 at 5:24
  • Awesome, glad you caught that and that it worked as desired! FYI, convention in WordPress is to have your post type singular. "post", "page", etc. – Matthew Boynes May 29 '13 at 13:09
  • 2
    It doesn't work for pages what shoul I change for having instead domain-name.com/parent/child/sub-child/ domain-name.com/sub-child/ or instead domain-name.com/parent/child/ domain-name.com/child/ – Sebastian Nov 18 '13 at 15:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.