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!

3 Answers 3


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.


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:

  • That makes a lot of sense, this stuff gets me a little confused after awhile. Cheers!
    – David
    Commented Mar 26, 2012 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?
    – TigrouMeow
    Commented Mar 26, 2012 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
    Commented Mar 26, 2012 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...
    – TigrouMeow
    Commented Mar 26, 2012 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
    Commented Mar 26, 2012 at 8:40

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

  • 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 :)
    – TigrouMeow
    Commented Mar 28, 2012 at 2:41

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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