How Posts in Sidebar chooses the main category of the post

A technical blog post on the new feature “Sort categories” in the Posts in Sidebar plugin.

One of the features of my plugin Posts in Sidebar is that the widget, when viewing a single post page (i.e. a blog post page), can display a content different from the standard. For example, a user could want to display a list of recent posts in the main widget but, when viewing a single post page, a list of posts published under the same category of the main post. The feature is available under Getting posts > Change the query when on single posts.

In a recent release of my plugin Posts in Sidebar (exactly the 3.8.4 version) I made an important change regarding which category to use in order to get the posts, when a post has multiple categories. In the previous versions the plugin picked up the category with the lowest letter in the alphabetically sorted list of the post’s categories and used it to get the posts. Starting from 3.8.4 the plugin can use the category with the lowest ID, that is the WordPress-style to choose the category for the permalink construction. Let’s see something more detailed on this.

Let’s say that a post is published under these two categories:

Category B where the ID assigned by WordPress is 100
Category C where the ID assigned by WordPress is 200

When we save the post, WordPress will use Category B as the main category of the post, because it has the lowest ID. This category will also be used in the permalink of the post so, if we use a permalink that has the category in it, the permalink we’ll be something as: http://www.example.com/category-b/title-of-the-post

Let’s say that, after some time, we change the categories of the post adding two new categories:

Category A where the ID assigned by WordPress is 300
Category Z where the ID assigned by WordPress is 50.

Now the post has four categories:

Category A where the ID assigned by WordPress is 300
Category B where the ID assigned by WordPress is 100
Category C where the ID assigned by WordPress is 200
Category Z where the ID assigned by WordPress is 50.

Now the main category will be Category Z, because it has the lowest ID (50).

Now comes the interesting part. Let’s say that we want to get the categories of the post because we want to display them in our theme or because we want that list to get related posts. When we retrieve the list of the categories of the post using a PHP function like wp_get_post_categories(), WordPress builds an array, alphabetically sorted, like this:

Array
(
    [0] => 300 (this is the Category A)
    [1] => 100 (this is the Category B)
    [2] => 200 (this is the Category C)
    [3] => 50 (this is the Category Z)
)

where the first numbers column is the ID of the array and the second numbers column is the ID of the category (in PHP, when you create an array, every line has its own ID). In our example, 300 is the ID of the Category A and this category has the lowest key ID in the array [0].

Now, which ID should Posts in Sidebar use to choose the category where to get posts from? The ID of the array or the ID of the category?

Until version 3.8.3 Posts in Sidebar retrieved the categories of the current post only considering the first column, that is the ID of the array. Among other things, this column of IDs could change in every moment: if we add another category to the post that has a name with a lower initial letter, this new category will be used! In the example above the plugin will use the Category A as main category, which has 300 as ID.

Starting from 3.8.4 version Posts in Sidebar has a new option for using the second column to determine the category where to get posts from. Also, this is the standard WordPress behavior when determining the main category of the post for permalinks. In the example above, if the user activates the new option “Sort categories” the plugin will use the Category Z as main category, which has 50 as ID.

In order to make this, I just changed the way Posts in Sidebar gets the categories of the current post. Now the plugin can sort the categories of the post from the lowest ID to the highest. In this way we are sure to get posts from the category used by WordPress in the permalink.

As always, this new behavior is not the default. The old way to determine the category is the default, but the user can choose the new way by activating the dedicated option.

Author: aldolat

A WordPress fanatic.

Leave a Reply

Your email address will not be published. Required fields are marked *