Skip to content


WordPress: tags from one category

I found myself with an interesting problem at another WordPress site I manage. It’s a link blog with a few thousand hyperlinks, and though the links are tagged, I wanted to manipulate those tags in a particular manner.

Here’s the situation: I wanted readers to be able retrieve posts with a similar tag filed only in a specific category. I’m sure there are sophisticated methods to achieve this but being a simple-is-best kind of guy, I took a kludgey route.

I previously mentioned how WordPress’s category templates make it easy to handle specific categories in a specific manner. You can do the same thing with tag templates.

Since I wanted WordPress to handle all tags in the same manner, I added a tag.php file to my theme folder and modified that to suit my needs. Here’s The Loop in the original tag.php:
< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>

< ?php the_content();?>

< ?php endwhile; else: ?>

< ?php _e('Sorry, no posts matched your criteria.'); ?>

< ?php endif; ?>

That tells WordPress to retrieve the content of every post with the selected tag from every category. I modified my tag.php file to this:

< ?php if (have_posts()) : ?>
< ?php query_posts($query_string . "&cat=258&showposts=10"); ?>
< ?php while (have_posts()) : the_post(); ?>

< ?php the_content();?>

< ?php endwhile; else: ?>

< ?php _e('Sorry, no posts matched your criteria.'); ?>
< ?php endif; ?>

(Note: due to formatting issues, you can’t simply copy-and-paste the above code. The syntax works, however.)

The key is to use the query_posts call to append additional conditions to the main query (which was “retrieve posts with the selected tag”). In my case, I got WordPress to retrieve posts with the selected tag and in a specified category. In the example above, the category has the ID of 258. You can modify that query_posts call to add or exclude categories as needed.

Update 21 July

There’s a minor issue with the above tag.php code. It doesn’t have a provision for posts that have the selected tag but aren’t in the specified category. For example, if I wanted tag.php to display all posts tagged “Transformers” and in the “Good Movies” category, I’d simply see an unhelpful blank space instead of the error message “Sorry, no posts matched your criteria.”

Because ob-viously there would be no posts tagged “Transformers” in the “Good Movies” category.

The solution is to add another check. The final code for The Loop in tag.php:
< ?php if (have_posts()) : ?>
< ?php query_posts($query_string . "&cat=258&showposts=10"); ?>
< ?php if (have_posts()) : ?>
< ?php while (have_posts()) : the_post(); ?>

< ?php the_content();?>

< ?php endwhile; else: ?>

< ?php _e('Sorry, no posts matched your criteria.'); ?>
< ?php endif; ?>
< ?php endif; ?>

Update 2 Aug.

Sigh. It occurs to me the second if…else loop is completely unnecessary if the additional condition is appended before going into The Loop.

The modified tag.php (take 3!):

< ?php query_posts($query_string . "&cat=258&showposts=10"); ?>

< ?php if (have_posts()) : while (have_posts()) : the_post(); ?>

< ?php the_content();?>

< ?php endwhile; else: ?>

< ?php _e('Sorry, no posts matched your criteria.'); ?>
< ?php endif; ?>

Posted in WordPress.