WordPress: Adding links to post categories
In WordPress 2.1, one of the features I was hoping for, with the new link categories, was the ability to add a link to a post category. Initially when I saw the new categories admin area, and the posts and links count columns, I assumed that it would be possible to add a link to the same category as a post.
For example: Let’s say I have a news category, and I want posts related to news and links to news sites to go into the same news category, rather than having a separate category news posts and news links.
Unfortunately, the category admin page in WordPress 2.1 is misleading. Even though, both link and post categories are stored in the same table, and are managed on the same page, the behavior is different. The way it works is, if you add a new category, and if the first item you add to the category is a link, then it becomes a link category, and if the first item you add is a post, then it becomes a post category. So if you go into the add link page, you will notice that the category list only displays the link categories and hides the post categories.
Now this behavior would be perfectly all right if you manage your links and posts as separate categories. But the way I do it in Counterjumper, posts and related links are usually assigned to the same category. A little digging around in the WordPress code, and it looks like this is an intentional behavior. The code can be found in return_link_categores_list() and return_categories_list() functions. These functions are in the admin-function.php file.
This is the SQL from return_link_categores_list function:
SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent AND ( category_count = 0 OR link_count != 0 OR ( link_count = 0 AND category_count = 0 ) ) ORDER BY link_count DESC
If you notice after the AND the first condition checks for categories with no posts, the second condition checks for categories with one or more links, and the third condition checks for categories with neither links nor posts. So, basically this condition controls the categories to which you can assign lists:
- Categories with no posts, OR
- Categories with one or more links, OR
- Categories with no posts and no links
A similar logic is used for getting a list of categories to which you can assign posts.
Solution
Now in my case I needed an immediate solution, so I decided to take out the above three conditions from the SQL statement, and this allowed me to add links to any category. Here is the trimmed SQL:
SELECT cat_ID FROM $wpdb->categories WHERE category_parent = $parent ORDER BY link_count DESC
I hope WordPress would remove this restriction on categories in the future, and allow it to be more flexible. As for the solution, it does change a core WordPress file, so I would not advise the use of this solution, unless you are comfortable changing things you should not be changing.




5 Comments
Feb 17, 2007 | 12:28 pm
Could you extend this logic to allow comments to be posted to categories as well? (Or /tag/ pages for those who use ultimate tag warrior). That would be wonderful to have.
Feb 17, 2007 | 2:24 pm
Hmm, that’s an interesting idea. You could follow the same logic and create a comment2cat table, and associate comments to categories. Since WordPress does not currently do this, you will have to make some kind of a custom plugin to accomplish this.
May 3, 2007 | 4:31 am
Thanks for detailing this. I’ve just spent the best part of a morning trying to work out why 60 categories weren’t showing up when posting/editing.
I hacked the code and found the same explanation as you.
It makes no sense to me, definitely counter-intuitive and even illogical.
There’s a bug report in Trac discussing this.
May 3, 2007 | 8:50 am
@TJ, do you have the Trac link for this bug?
Jun 19, 2007 | 4:07 am
[…] Wordpress installation by telling you how to actually FIX this problem. Instead, I’ll just point you in the right direction. In reality, this seems like a pretty simple thing to do. You just change an SQL query or two in a […]
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>