How can I change the category slug that appears in the URL of a post that’s in multiple categories? I’d like to change the default WordPress behaviour of using the category with the lowest id.
I thought this would do it:
add_filter('post_link', 'mspc_post_link', 1, 2);
function mspc_post_link($link, $post) {
if (strpos($link, 'bad-category-slug')) {
$cats = get_the_category($post->ID);
foreach ($cats as $cat) {
if ("Bad Category Name" != $cat->cat_name) {
$slug = $cat->category_nicename;
break;
}
}
$link = str_replace('bad-category-slug', $slug, $link);
}
return $link;
}
But while this kind of does work as expected, it doesn’t change the URL visible to user.
Similar plugin but compatible to latest wp version 3.5.2
http://wordpress.org/plugins/wp-category-permalink/
Even though this question is pretty old, and you’ve probably long moved on, I figured I’d offer a solution anyway :-).
Check out http://wordpress.org/extend/plugins/hikari-category-permalink/, which allows you to do exactly what you’re asking.
Cheers~