Support » Fixing WordPress » Custom permalinks for custom post type and taxonomy

  • I’m trying to create a custom permalink structure based on taxonomy terms. I’ve got it partially working, however I’m having a couple of issues that I can’t seem to figure out.

    Here’s my functions for registering my post type and taxonomy and creating the permalink structure:

    function create_services() {
        register_post_type('services', array(   'label' => 'Services','description' => '','public' => true,'show_ui' => true,'show_in_menu' => true,'capability_type' => 'post','hierarchical' => true,'rewrite' => array(slug=>'%locations%', with_front=>false),'query_var' => true,'exclude_from_search' => false,'supports' => array('title','editor','excerpt','trackbacks','custom-fields','comments','revisions','thumbnail','author','page-attributes',),'labels' => array (
            'name' => 'Services',
            'singular_name' => 'Service',
            'menu_name' => 'Services',
            'add_new' => 'Add Service',
            'add_new_item' => 'Add New Service',
            'edit' => 'Edit',
            'edit_item' => 'Edit Service',
            'new_item' => 'New Service',
            'view' => 'View Service',
            'view_item' => 'View Service',
            'search_items' => 'Search Services',
            'not_found' => 'No Services Found',
            'not_found_in_trash' => 'No Services Found in Trash',
            'parent' => 'Parent Service',
        ),) );
    add_action('init', 'create_services');
    function create_locations() {
        register_taxonomy('locations',array ( 0 => 'services',),array( 'hierarchical' => true, 'label' => 'Locations','show_ui' => true,'query_var' => true,'rewrite' => array(slug=>'', with_front=>true),'singular_label' => 'Location') );
    add_action('init', 'create_locations');
    add_filter('post_link', 'location_permalink', 10, 3);
    add_filter('post_type_link', 'location_permalink', 10, 3);
    function location_permalink($permalink, $post_id, $leavename) {
        if (strpos($permalink, '%locations%') === FALSE) return $permalink;
            // Get post
            $post = get_post($post_id);
            if (!$post) return $permalink;
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'locations');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'no-location';
        return str_replace('%locations%', $taxonomy_slug, $permalink);

    Problem #1: The permalink structure I’m trying to get for this post type is: — This seems to be working.

    However upon adding this to my functions, permalinks for pages and everything else stop working. They are using the permalink structure set in the WordPress settings of /%postname%/

    If I revert these settings back to the default, the pages and all show up fine, with exception of my custom post type permalink rewrite.

    Problem #2: If I were to create a post in my custom post type with the name “Plumbing” and assign it to a particular taxonomy term “Big Bear, CA”, my permalink shows This is great, however, if I create another post with the same name “Plumbing” but assign it to a a different taxonomy term “Victorville, CA”, my permalink shows

    How can I get the post name to not append the “-2”? Since it’s theoretically under a different url I would think that WordPress would be able to handle it..unless WordPress logic works differently.

    Is there a way to get clean, pretty URLs for this setup?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Here’s my .htaccess:

    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /dev2/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /dev2/index.php [L]
    # END WordPress

    On Problem 2, if the appendage of “-2” is unavoidable, could it be changed to be “-%taxonomyterm%”? So in that example, the output would be

    So! 🙂

    Did you ever get this working? Trying to accomplish the exact same thing and running into a lot of dead-ends.


    Now we have it working (technically), but the pages aren’t found when we go to a page from our custom taxonomy. The code we are using in functions.php:

    add_filter('post_link', 'custom_taxonomy_permalink', 10, 3);
    add_filter('post_type_link', 'custom_taxonomy_permalink', 10, 3);
    function custom_taxonomy_permalink($permalink, $post_id, $leavename) {
        // Get post
        $post = get_post($post_id);
        if (!$post) return $permalink;
        if ($post->post_type == 'reviews') {
            // Get taxonomy terms
            $terms = wp_get_object_terms($post->ID, 'manufacturers');
            if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug;
            else $taxonomy_slug = 'reviews';
            return str_replace('prreviews', $taxonomy_slug, $permalink);
        } else {
            return $permalink;

    And our register_post_type call:

    		'label' => 'Projector Reviews',
    		'description' => '',
    		'public' => true,
    		'show_ui' => true,
    		'show_in_menu' => true,
    		'capability_type' => 'page',
    		'hierarchical' => true,
    		'rewrite' => array('slug' => 'prreviews'),
    		'query_var' => true,
    		'exclude_from_search' => false,
    		'supports' => array(
    			'title','editor','page-attributes', 'comments'

    We chose to not use the %manufacturers% wildcard because it was causing a permalink conflict (, and we knew we’d be replacing it anyway so we chose a name that we hoped would avoid any conflict with anything in the %postname%.

    Any ideas/thoughts/insight? Thanks!


    I am also using the same my permalink is

    [type] is a non hierarchical custom taxonomy.
    It works fine when I use posts, but when I try to open pages it show not found.

    Is there any solution for this? I have tried it alot bit no success.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Custom permalinks for custom post type and taxonomy’ is closed to new replies.