1

In default wordpress support %category% tags in custom permalink structure. I need to use %taxonomy in permalink structure. How to do that?

Is there any filter are available?

3

There's an array option when creating a custom post type and custom taxonomy for ""rewrite"" and you can set a ""slug"" and it's there that you can change your permalink structure.

For more read here.

Also the other option that I get from post 1 and post 2 one helped me blink some idea.

1 Rewrite rule: You need to add a new rewrite rule so Wordpress knows how to interpret your url structure. In my case the custom post type part of the uri will always be the 5th uri segment, so I defined my match rule accordingly. Note that you may have to change this if you use more or less uri segments. If you'll have varying levels of nested terms then you'll need to write a function to check whether the the last uri segment is a custom post type or a taxonomy term to know which rule to add (ask me if you need help on that).

add_filter('rewrite_rules_array', 'mmp_rewrite_rules');
function mmp_rewrite_rules($rules) {
    $newRules  = array();
    $newRules['basename/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?custom_post_type_name=$matches[4]'; // my custom structure will always have the post name as the 5th uri segment
    $newRules['basename/(.+)/?$']                = 'index.php?taxonomy_name=$matches[1]'; 

    return array_merge($newRules, $rules);
}

2. Filter post type link: Then you need to add this code to let workpress how to handle %taxonomy_name% in your custom post type rewrite slug structure:

function filter_post_type_link($link, $post)
{
    if ($post->post_type != 'custom_post_type_name')
        return $link;

    if ($cats = get_the_terms($post->ID, 'taxonomy_name'))
    {
        $link = str_replace('%taxonomy_name%', get_taxonomy_parents(array_pop($cats)->term_id, 'taxonomy_name', false, '/', true), $link); // see custom function defined below
    }
    return $link;
}
add_filter('post_type_link', 'filter_post_type_link', 10, 2);

3. Custom function: I created a custom function based off of Wordpress's own get_category_parents.

// my own function to do what get_category_parents does for other taxonomies
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {    
    $chain = '';   
    $parent = &get_term($id, $taxonomy);

    if (is_wp_error($parent)) {
        return $parent;
    }

    if ($nicename)    
        $name = $parent -> slug;        
else    
        $name = $parent -> name;

    if ($parent -> parent && ($parent -> parent != $parent -> term_id) && !in_array($parent -> parent, $visited)) {    
        $visited[] = $parent -> parent;    
        $chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $separator, $nicename, $visited);

    }

    if ($link) {
        // nothing, can't get this working :(
    } else    
        $chain .= $name . $separator;    
    return $chain;    
}

4. Load again permalink setting page: Then you need to flush your permalinks.

Now everything 'should' work hopefully! Go make a bunch of taxonomy terms and nest them correctly, then make some custom post type posts and categorize them correctly. You can also make a page with the slug basename, and everything should work the way I specified in my question. You may want to create some custom taxonomy archive pages to control how they look and add some kind of taxonomy widget plugin to show your nested categories in the sidebar.

Taxonomy widget.

Hope this helps.

  • 1
    I'm having trouble trying to work out which parts I need to edit/replace with my own taxonomy name? – Pete Jun 27 '17 at 10:48

Your Answer

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

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