I have a Custom Post Type named "blog", and a Custom Taxonomy named "area" (both setup using the CPT UI plugin). I have also used below code to add the taxonomy slug in the url, so that standard WP POST will have the url structure: %area%/%post-title%/. What can I possibly add to below code so that my CPT named "blog" would also have the url structure example.com/%area%/blog/%post-title%/ if that's even possible? If not a structure of example.com/blog/%area%/%post-title%/ would be good too. P.S. the "area" taxonomy is available in both standard WP POST and CPT "blog". Much thanks guys!

add_filter('post_link', 'area_permalink', 10, 3);
add_filter('post_type_link', 'area', 10, 3);

function area_permalink($permalink, $post_id, $leavename) {
    if (strpos($permalink, '%area%') === FALSE) return $permalink;

        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;

        // Get taxonomy terms
        $terms = wp_get_object_terms($post->ID, 'area');   
        if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
        else $taxonomy_slug = 'primary_area';

    return str_replace('%area%', $taxonomy_slug, $permalink);

Your Answer

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

Browse other questions tagged or ask your own question.