I’ve always used get_posts to fetch posts by category, but recently I needed to show posts with a particular tagĀ and get_posts doesn’t seem to support this. Fortunately query_posts allows selection by tag as well as by category. Here’s the code I used:

<ul>
<?php query_posts('posts_per_page=5&tag=2'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(''); ?></a>
<?php the_post_thumbnail('') ?>
<?php the_excerpt('') ?>
</li>
<?php endwhile; endif; wp_reset_query(); ?>
</ul>