WordPress - creating a list of posts filtered by tag and then categories

I am trying to create a WordPress site with six lists per page, each list showing posts from a different category. Plain.

But then, if the user selects a tag by taking them to this tag archive page, I want them to still see the template with six lists, but all messages in each category are also filtered by the tag. Therefore, message lists are filtered first by tag, and then by category.

As far as I can tell, there is no way to do this with query_posts or something else, it needs more advanced use of the database, but I don't know how to do it! I think there is a similar question here, but since I know very little PHP and not MySQL, I cannot understand the answers!

+7
php mysql tags wordpress
source share
3 answers

Right, I finally found a relatively simple solution.

There is a bug in WordPress that prevents both categories and tags from being requested, so query_posts('cat=2&tag=bread'); won't work, but the way around this is query_posts('cat=2&tag=bread+tag=bread'); that magically works.

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

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

which works great.

+5
source share

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; 
+2
source share

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

Examples:

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

All Articles