Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Term_id as permalink for taxonomies WordPress


Hi there!

I'm trying to define a term_id as a permalink for taxonomies, as is post_id for posts.

I had not success with this. The code I have for a taxonomy called 'artist' is:

add_filter('post_type_link', 'artist_permalink', 10, 3);

function artist_structure(){
global $wp_rewrite;

$artist_structure = '/%term_id%/%artist%';
$wp_rewrite->add_rewrite_tag('%artist%', '([^/]+)', "artist=");
$wp_rewrite->add_permastruct('term_id', $artist_structure, false);
$wp_rewrite->add_permastruct('artist', $artist_structure, false);

function artist_permalink($permalink, $post_id, $leavename){
if (get_option('permalink_structure') != ''){
$post = get_post($post_id);
$rewritecode = array(
if (strpos($permalink, '%artist%') !== FALSE){
$terms = wp_get_object_terms($post->ID, 'artist');
if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $artist = $terms[0]->slug;
else $artist = '';
$rewritereplace = array(
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
return $permalink;


Answers (2)


Maor Barazany answers:

You have to flush your rewrite rules, try instead of


to have it as -

add_action('generate_rewrite_rules', 'artist_structure');

Or try to use flush_rewrite_rules()

See here in Codex, at bottom of page: [[LINK href=""]][[/LINK]]


rilwis answers:

Can you tell me what's your permalink structure for post?

And please explain more about the permalink structure for taxonomy. Suppose that you have a taxonomy "artist", and a term "britney" with ID = 10. So you want to display link (and make the link works) for "britney" is: