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

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


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.


Try this code:

query_posts('tag=selected_tag');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();            }        }endwhile;


According to the Wordpress API, you can filter by tags within a call to query_posts.

Examples:

query_posts('tag=cooking');query_posts('tag=bread,baking');query_posts('tag=bread+baking+recipe');