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

Term_id as permalink for taxonomies WordPress

  • REFUNDED

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('init','artist_structure');
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(
'%term_id%',
'%artist%'
);
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(
$terms[0]->ID,
$artist
);
$permalink = str_replace($rewritecode, $rewritereplace, $permalink);
}
return $permalink;
}


Thanks.

Answers (2)

2010-10-26

Maor Barazany answers:

You have to flush your rewrite rules, try instead of

add_filter('init','artist_structure');

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="http://codex.wordpress.org/Custom_Queries"]]http://codex.wordpress.org/Custom_Queries[[/LINK]]

2010-10-27

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:

domain.com/10/britney

or

domain.com/artist/10

???