Posts Tagged ‘Tags’

WordPress: How to scrape the Tags from Posts of a certain Category

Friday, May 8th, 2009

One of the features of WordPress is that it has a 2-D taxonomy built into it (taxonomy refers to the method of categorizing information).

The built in taxonomy consists of Categories and Tags.

For this site, which I am working on redesigning, I want to separate large portions of this site by category. For instance, I want a portion of the site to be information and articles focusing on Web Design and Development. I want another to be my personal blog. I may add others down the road, like a section displaying my art, and so on.

My plan was to distinguish these areas by assigning them different categories within WordPress.

This would only leave me to use Tags to further categorize content within these major areas.

In this way, I am really using Tags like Categories. Sure, this leaves me without the functionality of tags, but really, I don’t care.

So here’s the problem:

WordPress doesn’t really treat Tags and Categories as a true 2-Dimensional taxonomy system. The way WordPress functions, it is more like a 1-Dimensional system with two segments, and slightly different functions are built in to each.

I wanted to be able to pull a list of all the tags assigned to posts of a certain category, but this proved hard to do, or hard to find out how to do, with WordPress’s current functions and documentation.

I found a piece of code that did something similar as a starting point, and modified it to make it do what I needed it to do.

This code determines which posts are in a specified category or categories, determines the id numbers of the tags associated with each post, throws out duplicate tag ids, then lists all of the tags with those id numbers in an unordered list.

Here’s the code:


EDIT: I moved the unordered list html tags to within the IF statement, so that you don’t end up with an empty unordered list.

Shortcomings that I know of: The list is not ordered by anything except internal WordPress database IDs, so it appears to be in random order. Also, I don’t know how this works with child categories.

If you know of a better way to do this, let me know!