Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

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~

share|improve this answer
    
+1 - I've used this plugin before, and I works well. –  bungeshea Nov 1 '12 at 23:54
    
@bungeshea plugin warning seems to be like this > This plugin hasn't been updated in over 2 years. It may no longer be maintained or supported and may have compatibility issues when used with more recent versions of WordPress. –  Muhammed Athimannil Nov 30 '12 at 11:07
1  
@maanu Ignore the warning, it still works with WP 3.5 –  bungeshea Nov 30 '12 at 20:31
    
@bungeshea yes, it works :) Jazkallah khair –  Muhammed Athimannil Dec 1 '12 at 18:15

Similar plugin but compatible to latest wp version 3.5.2

http://wordpress.org/plugins/wp-category-permalink/

This plugin allows you to select a 'main' category for your posts, for better permalinks and SEO. It uses the same meta data as the "Hikari Category Permalink" and the "sCategory Permalink", but it has been rewritten using better and cleaner code.

Requires: WP 3.5 or higher Compatible up to: 3.5.2 Last Updated: 2013-4-18

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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