How To Exclude Pages From Taxonomy Counter In WordPress

How to exclude pages from taxonomy counter in WordPress

Recently I worked on a project where the client needed a way to categorize the pages in WordPress. They wanted to use the same categories which they use for their blog posts, so I thought that the proper way would be to enable the use of post categories on pages as well. Although this can be achieved easily, there’s is one problem that ocuurs when the post categories are used on pages, which I will explain in this post.

 

Enabling Categories for Pages

WordPress doesn’t offer usage of categories on pages out of the box, but this can be achieved quite easily by using a function called register_taxonomy_for_object_type. So to enable categories on WordPress pages we need to make a call this method with two arguments. First one is the name of taxonomy object which in our case is category, and the second one is the name of the object type for which we want to enable categories. In our case that is page.

The code for enabling categories for pages can be seen here.

After adding this code in let’s say functions.php file, the categories meta box will be displayed in page edit screen and we will be able to categorize each WordPress page.

 

The Problem

The categories(and other taxonomies) in the WordPress has a mechanism for counting how much of posts are assigned to each category. Each time one post is assigned to the category the counter increases by one and decreases by one if the post is removed from the category. This enables widgets like “Categories” to show post count on the front-end, and to only display a list of categories which are not empty.
The problem might ocour if we want to display list of categories which have posts attached to it on our blog.
Let’s say that we for example have one category called “Movies” and other one “Books”. We might create new post about the movies and attach it to the “Movies” category. If we then open our blog page on which we use “Categories” widget in sidebar, there will be only “Movies” category listed.
If we decide to create a new page and attach it to the “Books” category and then open blog page, both of the categories will be listed even if we didn’t attach any post to the “Books” category.
This is not so huge problem, but it doesn’t look good if someone clicks on the “Books” category and see that there’s no posts in that category.

 

Solution

The solution that I’ve come up with for this problem is not very pretty, but it does do his job. It prevents the counting of the pages attached to categories and by doing that it solves the problem addressed above.
My solution is adding a callback function for updating the counter, which will call the unregister_taxonomy_for_object_type function before process of the counting of posts and pages begins.
Then after the process of counting finishes, the register_taxonomy_for_object_type will be called again.
This way the counter mechanism doesn’t take pages into account and counts posts only.

 

Conclusion

Using the few lines of code I managed to exclude the pages from the taxonomy counter and remove empty categories from widget “Categories” widget. This solution can be applied to other post types and taxonomies as well.
If you think that this could be done in a different way leave the comment below. I would like to see different solutions.

About the Author

Freelance WordPress developer who offers full stack web development services. Occasionally writes about business and WordPress development. When not in front of the computer can be found on a bicycle or enjoying other outdoor activities.

Leave a Reply 0 comments

Leave a Reply: