WordPress - producing a list of posts filtered by tag and then category


I'm trying to make a WordPress site that has six lists on a page, each list showing posts from a different category. Simple.

But then, if a user selects a tag, taking them to that tag archive page, I want them to still see the six-list template, but all the posts within each category are also filtered by the tag. So lists of posts are filtered first by tag, and then by category.

As far as I can tell, there is no way of doing this using query_posts or anything, it needs more advanced use of the database, but I have no idea how to do this! I think that there's a similar question on here, but because I know very little PHP and no MySQL, I can't make sense of the answers!

6/18/2009 6:11:56 PM

Accepted Answer

Right, I have finally found a relatively simple solution to this.

There's a bug in WordPress preventing a query of both category and tags working, so query_posts('cat=2&tag=bread'); wouldn't work, but a way around this is query_posts('cat=2&tag=bread+tag=bread'); which magically works.

In a tag.php template, I wanted it to pick up the tag from that archive, so I had to do this:

<?php query_posts('cat=12&tag='.$_GET['tag'].'+'.$_GET['tag']); ?>

which works perfectly.

8/26/2009 1:56:17 PM

Try this code:


while (have_posts()) : the_post();

    foreach((get_the_category()) as $category)

        if ($category->cat_name == 'selected_category')
            // output any needed post info, for example:
            echo the_title();



Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow