I need to list posts by category, but only for posts which have a given tag (or tags). So far I have the following code, which works fine for listing ALL my posts grouped by category, but I’m not sure how to modify it to only select posts with a given tag. What should I change?
<?php
// get all the categories from the database
$cats = get_categories();
// loop through the categries
foreach ($cats as $cat) {
// setup the cateogory ID
$cat_id= $cat->term_id;
// Make a header for the cateogry
echo "<h2>".$cat->name."</h2>";
// create a custom wordpress query
query_posts("cat=$cat_id&post_per_page=100");
// start the wordpress loop!
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php // create our link now that the post is setup ?>
<a href="<?php the_permalink();?>"><?php the_title(); ?></a>
<?php echo '<hr/>'; ?>
<?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
<?php } // done the foreach statement ?>
All you need to do is edit your query to include the tag, i.e.
query_posts("cat=$cat_id&tag=tag1+tag1&showposts=100");Note you need all of
tag1+tag1