Removing parent slug from URL on custom post type

The question:

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

Something that would change this:

http://demo.com/parent-service/child-service-1
http://demo.com/grand-parent-service/parent-service/child-service-2
http://demo.com/great-grand-parent-service/grand-parent-service/parent-service/child-service-3
etc

To something like this:

http://demo.com/child-service-1
http://demo.com/child-service-2
http://demo.com/child-service-3
etc

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.

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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 );


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment