WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I assign a post to two categories always one of categories becomes the main category.

Is there any way to specify which category out of the two or more is the main category not a secondary category?

share|improve this question
up vote 2 down vote accepted

If by "main" category you mean the category that is used to create the permalink, the default is to use the category with the lowest ID. You can access the post also with an URL that contains another category, but the rewrite_canonical() function will kick in and redirect you to the "canonical" URL with the "main" category.

However, if you hook into the get_permalink() function and return a URL based on another category, the canonical rewriter will notice this and it won't redirect.

So you will need to create a UI in the post creation screen to select the "main" category, and hook into post_link to create URLs with this category.

share|improve this answer

What do you mean by main? I don't think there is such concept in post's data, but it may look like it for some purposes (for example category in permalink). Categories are retrieved in certain order - likely in order they are stored in database, equals order they were created in.

share|improve this answer
You are correct the "main" category is determined by the ID of the category. So the "main" category is always the one with the lowest ID number. Is there any way to overwrite this default behavior? – Eeyore Feb 28 '11 at 3:34
@Eeyore I hadn't looked at related code by suspect this is going to be impractical to handle globally. What are your specific needs/context where you want to control this? – Rarst Feb 28 '11 at 15:18

Your Answer


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.