i have been struggling with querying posts using tags and have found no solutions whatsovever. My idea is to query all wordpress posts having the same tags using query_posts and this just not working and giving weird results
query_posts('cat=-53&tag=best php scripts');
query_posts('cat=-53&tag=best+php+scripts');
query_posts('tag=best,php,scripts');
and tried every combination and it seems to be issue with wordpress 3+ version. Combining with categories also not working.
I have also tried using this code in the single.php page by retrieving tags and querying for posts. It also doesnt work.
$tags = get_the_tags($post->ID);
if ($tags) {
foreach($tags as $tag) {
$tag = $tag->name . '';
}
}
query_posts('cat=-53&tag='.$tag.'');
Still havnt found any solution for this myself.
Similar Posts:
- How to show adsense on specific posts/pages in WordPress?
- WordPress similar posts plugin not working?
- How to output category name in WordPress?
- 3 Must Do Things for WordPress Blog Starters
- How to post source code in WordPress posts?
- WordPress – How to get category ID for use in category.php
- How to redirect when post submitted for review in WordPress
- How to remove GET variable within $_SERVER['QUERY_STRING'] in PHP
- How to filter & escape data from Injection attacks in PHP!
- Fix -> WordPress wp-comments-post.php is blank !


December 7, 2010
I’m no expert on WP but I can see that code snippet is bad for a number of reasons – assigning to the loop variable $tag (?), not comma (or +) separating the list you are trying to build, and not using the slug (name can contain spaces which won’t work).
There’s maybe a better way of doing this, but you can do:
$tagList = ”;
$tags = get_the_tags();
if ($tags) {
foreach($tags as $tag) {
$tagList = $tag->slug . ‘,’;
}
}
$tagList = trim($tagList, “,”);
query_posts(“tag=” . $tagList);