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.

I'm using the permalink structure "/%category%/%postname%/".

What bothers me is that I have posts that belong to two categories. And of course, unfortunately, Wordpress always picks the wrong one for the slug generation.

Is there a nice and clean way of having control on which category will be chosen for the post slugs?

Thanks a lot!

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

WordPress picks the lowest ID as its main permalink. However, ALL categories will contain your post automatically. Meaning both these:

...will return your post (given that post-A is in both category-A and category-B) via a redirect. It can't list them all to you on the Edit page, sorry. But it will make sure they're available in all categories.

Of course, get_permalink will return the wrong one for you.

http://codex.wordpress.org/Using_Permalinks#Using_.25category.25_with_multiple_categories_on_a_post

Now as for overriding the behavior, look at:

http://core.trac.wordpress.org/browser/tags/3.3.1/wp-includes/link-template.php#L71, specifically a little lower on line 121. It's going to use $cats[0]->slug after sorting by ID. Which does not appear to be hookable in any way inside there, so either higher or lower.

This is a ticket that was going to change the way it worked http://core.trac.wordpress.org/ticket/18752 it's marked for Future Release, though. Subscribe to the ticket to show your interest, maybe it makes it into WordPress 3.5 later this year.

The pre_post_link filter will allow you to override the permalink. post_link, too.

Alternatively, get_the_category, should allow you to hack the IDs for a post (or remove the others to leave only one), but this should only occur when the permalink is requested.

add_filter( 'pre_post_link', function( $permalink, $post ) {

     if ( strpos($permalink, '%category%') === false )
         return; /* not interested */
     add_filter( 'get_the_categories', 'wpse46860_prune_categories' );
     return $permalink;

}, null, 2 );

function wpse46860_prune_categories( $categories ) {

    /* run once */
    remove_filter( 'get_the_categories', 'wpse46860_prune_categories' );

    /* .. prune the categories to leave the one you need .. */
    global $_hijack_post_id;

    return $categories;

}

This is a hack.

An additional difficulty is to get the post_ID to prune the categories against, the get_the_categories filter does not pass the post_ID to us; use a global (dirty) or a class property (clean).

You can try out different permalink plugins like:

share|improve this answer
    
That makes a lot of sense, this stuff gets me a little confused after awhile. Cheers! –  David Mar 26 '12 at 8:03
    
I knew that, and a few months ago I re-created my categories to have them "in order". But now I have many posts and many categories, and I wondered if there was a plugin for that? –  Jordy Meow Mar 26 '12 at 8:07
    
@TigrouMeow I'm updating the answer little by little in search for a good solution. There appear to be no plugins for this. Except maybe wordpress.org/extend/plugins/custom-permalinks which is isn't too nice. –  soulseekah Mar 26 '12 at 8:10
    
That could be a good hack. I could maintain a list of categories to exclude (as an option in my theme for example) and use it in this second function. But again, if can I assign very high IDs easily to those categories, the final result will be the same, and I wouldn't have do any kind of hacks... –  Jordy Meow Mar 26 '12 at 8:39
    
@TigrouMeow, so the short answer appears to be "not yet", core.trac.wordpress.org/ticket/18752 was supposed to handle this, still waiting. Other than that hope my hacks helped you understand how it all works. –  soulseekah Mar 26 '12 at 8:40
show 3 more comments

You can always go with a plugin. Hikari Category Permalink does exactly what you are looking for... with no hassle.

share|improve this answer
    
Super plugin! That's actually what I was looking for. Really clean and convenient. I hope it will be kept updated though, as I don't know how it operates exactly. Thanks for this :) –  Jordy Meow Mar 28 '12 at 2:41
add comment

IF you can save Which category you want as default permalink, then you can hook into get_permalink function using "pre_post_link" filter....

This might also help, give a look.. http://shibashake.com/wordpress-theme/wordpress-permalink-add

share|improve this answer
add comment

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.