Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

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 ),
share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks, that works to get the taxonomy name 'division' into the url, but what I'd like is for 'division' to be replaced by it's category name. –  maikunari Sep 19 '11 at 21:30
    
OK. Update your question to make that clear. I'll update my answer with the revised solution. –  marfarma Sep 20 '11 at 5:51
    
Thanks so much for the response. It worked perfectly - although I did need to add 'division/%division%/' will note this in my question. –  maikunari Sep 20 '11 at 16:12
    
Hello, I am following your approach but I am always getting "division-missing" in my URL. What can cause this problem? thank you –  whiteletters and blankspaces Apr 17 '13 at 21:23
    
I'm not actively working with WordPress at the moment, so won't be able to take a look at this. You should create a separate question, reference this answer and include more detail of what's not working, and maybe someone else will be able to help. If you do figure out what was wrong, missing or 'not clear' - please comment back here to help the next person. –  marfarma Apr 17 '13 at 21:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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