I have custom post types for “property” and “apartment” and a custom taxonomy “property-location” for the property post type.
There is a custom field (ACF) named “property_relation” in apartment post type which stores the property the apartment belongs to.
So far so good.

Now I added rewrite rules for the permalinks, which are:
location: all-locations/%postname%
property: all-locations/%location%/%postname%
apartment: all-locations/%location%/%property%/%postname%

and added the following functions to rewrite the slugs:

function my_custom_rewrite_tag() {
  add_rewrite_tag('%location%', '([^&]+)');
  add_rewrite_tag('%property%', '([^&]+)');
}
add_action('init', 'my_custom_rewrite_tag', 10, 0);

function my_custom_rewrite_rule() {
    add_rewrite_rule('all-locations/([^/]+)/?$', 'property-location=$matches[1]', 'top');
    add_rewrite_rule('all-locations/([^&]+)/([^&]+)/?$','location=$matches[1]&property=$matches[2]','op');
    add_rewrite_rule('all-locations/([^&]+)/([^&]+)/([^&]+)/?$','location=$matches[1]&property=$matches[2]&apartment=$matches[3]','top');
}
add_action('init', 'my_custom_rewrite_rule', 10, 0);

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

    if ( $prop_id = get_field('property_relation', false, false))
        $property = get_post_field('post_name', $prop_id);
        $terms = wp_get_object_terms( $prop_id, 'property-location' );
        $location = $terms[0]->slug;

        $post_link = str_replace( '%location%', $location, $post_link );
        $post_link = str_replace( '%property%', $property, $post_link );

    return $post_link;
}
add_filter('post_type_link', 'apartment_post_link', 1, 2);

function property_post_link( $post_link, $post ) {

    if ( $post->post_type != 'property')
        return $post_link;

    if( $terms = wp_get_object_terms( $post->ID, 'property-location' ) )
        $location = $terms[0]->slug;

    return str_replace( '%location%', $location, $post_link );

}
add_filter( 'post_type_link', 'property_post_link', 1, 2 );

all-locations is a page, which works fine
all-locations/location-name also works fine (custom taxonomy template)
all-locations/location-name/property-name also works fine (single-property template
but all-locations/location-name/property-name/apartment-name works only in theory (backend and frontend links show correct), should use the single-apartment template but produces a 404

Obviously I flushed the rewrite rules many times, otherwise the location and property links wouldn’t work.

I am not using additional query vars as I am fine with ugly permalinks just stating /?apartment=apartment-name and so on. And that’s exactly the url that displays the single apartment even when I am in pretty-permalink mode.
So now I am not sure how to get the apartment link working correctly… any ideas or suggestions?

Read more here: Custom post type permalink structure with ACF field value


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: