If a post belongs to two categories how do I choose the main category?

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?

Related posts

Leave a Reply

3 comments

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

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