the query expects the tag slug, not the tag name:
$tag = $tag->slug . ' ';
and there is an error in the query syntax; corrected:
query_posts('tag='.$tag.'&posts_per_page=-1');
edit:
full code rearranged:
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tag = $tag->slug; break;
}
query_posts('tag='.$tag.'&posts_per_page=-1');
while (have_posts()) : the_post(); ?>
<p><a href="https://wordpress.stackexchange.com/questions/27106/<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
<?php endwhile;
wp_reset_query();
}
Related Posts:
- how to query posts by category and tag?
- How to return results of a get_posts() in explicitly defined order
- Modify main WordPress loop with a parse_query filter
- How should I intercept the main query and inject custom join / order by / group by criteria
- Filter query_posts by tag slug on “Tag Archive” page (when tag is 2 or more words)
- Dynamically excluding current page id
- How to order results by date and meta key?
- Static Front-Page Excerpts
- Is it possible to query_posts using post__in and then Loop through them in the ordered they were queried?
- query_posts() ALWAYS displays something?
- How to remove a plugin filter’s priority on specific loops (custom queries)?
- Custom loops, sticky posts, and pagination nightmare
- Filter Loop by Custom Field Value
- Modify loop but keep the original query, what am I doing wrong?
- When to use WordPress loop or Foreach loop?
- Post per page not working if no category specified
- Get posts that do not have the same tags as current
- Calling Posts from Specified Cat
- Query Posts Incorrectly Displaying Tags Archives
- Most Effective Method? Exclude Category and Number of Posts Per Page
- How do I query based on the modified date?
- How to add an “or” instead of and “and” (&) in a WordPress query?
- Modify main loop query for paged and meta key
- Create a Loop with Posts Ordered by Most Recent Comments
- No duplicate tags by category
- query_posts and sub pages?
- The_content display a different content from a previous loop
- How to go to tag archives using a form
- WordPress loop problem: Multiple loops, index.php and is_paged causing duplicate posts on next page
- Including post data in a sidebar occurring outside of and before the Loop
- Multiple loops with / without sticky posts and different post limits
- How to do query_posts on tags pages
- query_post by title?
- How to make “sticky” pages (and query by them)
- Problem with ‘post__not_in’
- Sort posts alphabetically by custom field value, insert divider between different letters
- Retrieving 3 latest post from each of 5 different custom post types
- Custom query with query_posts doesn’t show post without certain meta_key
- Can’t show comments count per post outside loop
- How to sort by meta value?
- pagination in author.php template returns 404 error
- Display one latest post from multiple categories
- query recent posts from several categories
- Example Query Posts Showing the Latest Post with the Featured Image
- How to Loop within a Loop (Display Children and then Grandchildren)
- posts_per_page => 1 shows 2 posts
- Show individual author rank from query of cumulative post view count for all authors
- Get titles of all posts with current tags except current post
- Custom posts per page does not work with pagination
- posts_per_page override by default settings
- How to paginate posts correctly that belong to a particular category and are random ordered
- Tricky WP Query
- Change or update WordPress loop based on dropdown selection
- tax_query operator woes
- How to filter query to display posts from parent category only?
- WP_Query condition affects posts_per_page count
- How To Get Posts With A Specific Word
- Display random post on a page with post permalink in URL
- Query Posts Exclude Entire Category
- WordPress pagination with get_posts?
- Using ajax with paging and a custom sub-query
- query_posts() on key’s value, or key’s existence
- How to display certain category in the loop from WordPress default post type?
- How to display posts by vote count and if no value continue with latest posts with no votes?
- Display custom post type by custom taxonomy
- WordPress wp_get_attachment_thumb_url full
- How can I access specific posts brought back by query_posts?
- How to change default post type / post to media or attachments
- Include future posts in tags and in search
- postsperpage value not being applied
- Query Products & Store IDs in array
- Creating attachments archive in tags and categories
- query_posts orderby multiple ways
- Can I use WP_Query one time and then filter the results multiple times
- Pagination linking to first page
- Posts being viewed
- Using has_tag() outside loop
- show all in query_posts on category.php?
- Best Query for blog posts
- Duplice post with standard WP loop – fixed by using query_posts() instead
- how to add 4 post in one carousel slide
- Custom query for custom post type not getting correct post ID
- Can I Paginate Post after every 25 tag?
- Create a hierarchical loop at predefined markup requirements
- Modify WordPress loop after it has been run
- WordPress Custom Post Type and sort by Tags
- Secondary loop cuts off at 10 posts?
- query_posts not working after wordpress 3.7 update
- Post Filter clearing on pagination $_post
- Is that the query_posts() the real criminal here?
- single.php fires more than once after clicking on any post to view with different post id each time
- Query Custom Post Type by Tag
- Display by tag from different post types
- Least Number of Loops to Create Custom Homepage?
- Query specific posts from parent by slug rather ID
- How to refine WP-Query with further criteria
- prevent display duplicate titles on main page
- Pagination in category.php not functioning
- Lots of SQL queries
- Show number of posts AND number on current page (cannot make it work)