After a pretty exhaustive search, I'm stumped.

I have a Wordpress custom post type named: listings. Listings have taxonomies called Services and Areas.

The default structure is


I'm trying to achieve a structure that looks like:


I was able to achieve something close using the plugin Custom Post Type Permalinks which gives me this working URL:


And then used the code

function gp_remove_cpt_slug( $post_link, $post, $leavename ) {

    if ( 'listing' != $post->post_type || 'publish' != $post->post_status ) {
        return $post_link;

    $post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );

    return $post_link;
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );

That shows a permalink of what I want when I edit the listing but there's a 404 error when I go to that URL. I'm not sure where to go from here. Please help.

1 Answer 1


A simple solution would be using the rewrite and 'with_front arguments while registering the post type. Try this..

'with_front' => false,
'rewrite' => array( 'slug' => '%services%/%areas%'),

Then add a filter to post_type_link to replace the tags with terms.

function wpse277778_rewrite_urls( $url, $post ) {

   if ( 'listings' == get_post_type( $post ) ) {
        $services = wp_get_post_terms( $post->ID, 'services' );
        $areas = wp_get_post_terms( $post->ID, 'areas' );

        $url = str_replace( '%services%', $services[0]->slug, $url );
        $url = str_replace( '%areas%', $areas[0]->slug, $url );

    return $url;
add_filter( 'post_type_link', 'wpse277778_rewrite_urls', 10, 2 );

Please note, this is a barebone code and you need to do some checking with conditional tags to output error free $url. Also, you should set a default services and area in case no terms are associated.

  • After a bit more work, I discovered this solution is causing a 404 error for pages. Most of the discussions I found suggested it was a conflict with a page/post. Since I have a 'services' called hotel and a page called hotels, the /hotel/ was redirecting to /hotels/. I implemented code to prevent predictive redirects. That didn't fix it. So the CPT permalink works but pages don't. I also tried doing %services%-%areas% instead of a / between the two. The pages and CPTs now work. This solution works good for me. However, now there's no archive page for %services%-%areas% Suggestions? Commented Aug 24, 2017 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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