Jun 28, 2011

Wordpress-Add custom taxonomies tags to permalinks

Hello everyone, today we are going to discuss with you how to add wordpress custom taxonomies to permalinks.
You can refer custom taxonomies or taxonomies from here

Ok first of all, you need to register custom taxonomy with setting "rewrite=true", doing this will enable %custom_taxonomy_tag%
If you want to add custom taxonomies without coding, i prefer this plugin GD CPT tools

After doing all these things, you need to copy paste this function, to your theme's function.php file
[php]add_filter('post_link', 'taxo_permalink', 10, 3);
add_filter('post_type_link', 'taxo_permalink', 10, 3);

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

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

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

return str_replace('%taxo%', $taxonomy_slug, $permalink);
Line 5 – If the permalink does not contain the %taxo% tag, then we don’t need to translate anything.
Line 12 – Get the taxo terms related to the current post object.
Line 13 – Retrieve the slug value of the first taxo custom taxonomy object linked to the current post.
Line 14 – If no taxo terms are retrieved, then replace our taxo tag with the value no-taxo.
Line 16 – Replace the %taxo% tag with our custom taxonomy slug.

Now in wordpress, dashboard-settings-permalinks, you can write
This way you can add more custom taxonomies, just by adding more functions.
So Simple!!!!!!!


  1. you are a god among men, i searched for hours for this to get my wordpress working. thank you so much!

  2. I believe you should retrieve the first term object using array_shift, since the first taxonomy will not always be in the "0" position of the array. It would be in its ID position