Custom Taxonomy URL

I have been reading all of the similar answers on here for hours but can’t find anything basic enough for my needs.

I have a non-hierarchical custom post type “inventory”. I have one custom taxonomy “division” which is the inventory category

Read More

I’d like the permalink URL for a single page to be:

sitename.com/division/postname

but it only comes up as:

sitename.com/postname

What is the simplest way to get the inventory category (division) to display in the url?
Just to clarify, I’d like to have ‘division’ replaced with the name of the category that division represents.

Suggestions are much appreciated!

Update:

marfarma’s answer below worked perfectly, note that it was necessary to include the static string before the dynamic portion:

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

Related posts

Leave a Reply

1 comment

  1. If all you wanted was ‘division’ in the URL, the trick is to register the custom post type with the slug option:

        'rewrite'   => array( 'slug' => 'division', 'with_front' => false ),
    

    However, since you want to substitute the current value of the division tag for the word division, the answer is much more interesting.

    What you need to do is to declare your own rewrite tag: %division%

    global $wp_rewrite;
    $wp_rewrite->add_rewrite_tag('%division%', '(.+?/)?', 'division=');
    

    Then your slug is declared like this:

        'rewrite'   => array( 'slug' => '%division%', 'with_front' => false ),
    

    Then you have to hook the post_type_link function to enable the tag to be resolved.

    add_filter('post_type_link', 'division_permalink', 10, 3);
    
    function division_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, '%division%') === FALSE) return $permalink;
    
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
    
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'division');    
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'division-missing';
    
        return str_replace('%division%', $taxonomy_slug, $permalink);
    }
    
    

    Note: Untested code, please let us know if you needed to fix anything to get it to work. In particular, I would typically use the form division/%division% – so it is possible that this code may not work without a static string to sit in front of the dynamic portion, i.e. a URL like this: division/%division%/page.php

    Useful ‘custom rewrite tag for custom post type’ tutorials on the web:

    Interesting Custom Post Type Slug with Taxonomy and Custom Field

    http://xplus3.net/2010/10/04/wp-rewrite-tags-in-permalinks/

    http://shibashake.com/wordpress-theme/add-custom-taxonomy-tags-to-your-wordpress-permalinks

    Additional Note:

    If the code provided above doesn’t work without a static prefix (i.e. ‘division/%division%’), and you really need that, then I suggest a close read of this plugin’s code:

    http://wordpress.org/extend/plugins/wp-no-category-base/

    It’s only 93 lines long (and 26 of them are comments that identify the plugin and specify GPL licensing). If you can replicate that logic for your CPT, in conjunction with the above, I suspect that it will work.