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?


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.

| 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.

| 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

This mechanism is provided by the plugin Yoast SEO. It allows you to select or change the "main" category that is used to create the permalink.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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