Title: query_posts outside the loop?
Last modified: August 18, 2016

---

# query_posts outside the loop?

 *  Resolved [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * (@peacefulkarl)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/)
 * I have placed a query_posts element outside the loop in the header so wordpress
   is calling that tag instead of the loop tag. I am using it to create a list of
   recent posts. Is there another way of doing this?
 * Thanks Karl

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Chris_K](https://wordpress.org/support/users/handysolo/)
 * (@handysolo)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543113)
 * Should we assume this is a dupe of [http://wordpress.org/support/topic/111724?replies=1](http://wordpress.org/support/topic/111724?replies=1)
   or are there more details that you haven’t provided yet?
 *  Thread Starter [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * (@peacefulkarl)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543114)
 * Handy, I’ve been searching everywhere for an answer for this and just need a 
   little ‘nudge’. I guess you might call it a dupe but I prefer to think of it 
   as a call for help…
 * I have tried
    <?get_posts(‘showposts=6’;?> and it returns only one post…
 * I have tried
    <?php wp_get_archives(‘type=postbypost&limit=6’); ?> and it returns
   6 posts but without dates preceding the title…
 * I have a link below that points to the permalink and
    <?php the_time(‘m.j’) ?
   ></span> <?php the_title() ?> to get the date and title…
 * what am I missing here?
 * sorry to be that guy that wont quit posting, I swore I would never be him… But
   tonight I have no answers…
 *  [alakhnor](https://wordpress.org/support/users/alakhnor/)
 * (@alakhnor)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543116)
 * The correct syntax is :
 *     ```
       $myposts= get_posts('numberposts=6');
       foreach ($mypost as $post) :
          [bla...bla]
       endforeach;
       ```
   
 *  Thread Starter [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * (@peacefulkarl)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543118)
 * I tried it but got an error message. I am trying to put it in the side bar… does
   that matter?
 *  Thread Starter [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * (@peacefulkarl)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543120)
 * It looks right in the navigation bar at the top right now but is reeking havoc
   on every page but the index because the query_posts element is outside the loop…
 *  Thread Starter [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * (@peacefulkarl)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543194)
 * OK… I got it running sort of… my code looks like this…
 * <?php$myposts= get_posts(‘numberposts=6’);?>
    <? foreach ($myposts as $post) :
   if (have_posts()) :while (have_posts()) : the_post(); ?> [ bla… bla…} <?php endwhile;
   endif: ?> <?php endforeach;?>
 * it has solved part of the problem but now the list only shows the latest post
   over and over instead of the last six different posts… what did I miss in there?
 *  Thread Starter [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * (@peacefulkarl)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543212)
 * WOOOHOO It took some doing but I got it….
    I am no php wizard but your code works…
 * thanks
    alakhnor
 * Karl

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘query_posts outside the loop?’ is closed to new replies.

## Tags

 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [posts](https://wordpress.org/support/topic-tag/posts/)
 * [query_posts](https://wordpress.org/support/topic-tag/query_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 3 participants
 * Last reply from: [peacefulkarl](https://wordpress.org/support/users/peacefulkarl/)
 * Last activity: [19 years, 2 months ago](https://wordpress.org/support/topic/query_posts-outside-the-loop/#post-543212)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
