Title: using get_posts before loop
Last modified: August 18, 2016

---

# using get_posts before loop

 *  [Michael Bishop](https://wordpress.org/support/users/miklb/)
 * (@miklb)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/)
 * I’m working on a theme from a [design at oswd.org](http://www.oswd.org/design/preview/id/2265),
   and the design has a list of recent posts above the header. I’m able to get two
   columns of 3 posts titles with links no problem using the get_post template tag:
   `?
   php $posts = get_posts('numberposts=3'); foreach($posts as $post) : setup_postdata(
   $post); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></
   li> <?php endforeach; ?> and adding an offset= in the second column. However,
   no matter what I do, I’m getting a mysql database error when the page lists the
   posts in the content. Remove the get_posts queries, and the error goes away. 
   [Example here](http://sandbox.miklb.com/) What am I missing. This code works 
   fine in the sidebar, can it simply not be used before the loop? **EDIT** I am
   using 2.0 on this testbed site, but the error occured on a 1.5.2 site as well.

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

 *  Thread Starter [Michael Bishop](https://wordpress.org/support/users/miklb/)
 * (@miklb)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297759)
 * To update, with a lot of help of alphoide via IRC, we got to the point using 
   a query_posts before the full “loop” in my index.php file made all the DB errors
   go away, however, when clicking on the first post to get to single post view,
   it takes you to the post from the offset. So basically, I need to figure out 
   how to “un-offset” the posts afterward. any suggestions. Otherwise, I’m gonna
   scrap the second loop and put something else in the element.
 *  [Kassad](https://wordpress.org/support/users/kassad/)
 * (@kassad)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297776)
 * I may be misunderstanding the problem but it seems to me either a multiple loop
   question or more like a simple multiple sidebar question.
 * I would just introduce a new instance of sidebar and position it according to
   the design.
    It is really an unusual positioning of a sidebar but seems like 
   the most natural approach.
 *  Thread Starter [Michael Bishop](https://wordpress.org/support/users/miklb/)
 * (@miklb)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297778)
 * Actually kassad, it’s a multiple loop question. I’ve changed the elements around,
   so actually the example is longer valid. The design is the same, but i’ve moved
   the tag cloud to the area which I was trying to run the two get_posts loops. 
   Ultimately what was happening, was everything was good, but the second get_post
   loop was using an offset. Then when I ran my “normal” loop in my index.php, and
   you clicked on the first post for single post view, the permalink would be right,
   but it was taking you to the offset post, that is, if my offset was 3 in my header,
   second get_post loop, then when you click on the first post of the index section,
   it was taking you to the 4th post. It’s very confusing to explain, and more so
   for my skill level, thus a reuse of the space.
 *  Thread Starter [Michael Bishop](https://wordpress.org/support/users/miklb/)
 * (@miklb)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297779)
 * Ah, now that I re-read what you are saying, rather than code the space in the
   header, which comes before the loop in the index.php, treat it as sidebar and
   position it above the content via CSS. Possible, but I’m liking the idea of the
   tag cloud up there anyway. But thanks for offering the suggestion. Still doesn’t
   fix the multiple loops dilemma.
 *  [Kassad](https://wordpress.org/support/users/kassad/)
 * (@kassad)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297799)
 * I have not yet used this method so I have not much to offer.
    Though there are
   twe threads discussing similar problem:
 * [http://wordpress.org/support/topic/30611](http://wordpress.org/support/topic/30611)
 * This, at the end, refers to:
 * [http://wordpress.org/support/topic/28203#post-193667](http://wordpress.org/support/topic/28203#post-193667)
 *  Thread Starter [Michael Bishop](https://wordpress.org/support/users/miklb/)
 * (@miklb)
 * [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297818)
 * yeah, I’m guessing the best option would have been to scrap the get posts, and
   use query_posts and the multiple loops models. I simply had never tried to run
   another loop after the get_posts, so I didn’t realize its “_power_” 😉

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

The topic ‘using get_posts before loop’ is closed to new replies.

## Tags

 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [Michael Bishop](https://wordpress.org/support/users/miklb/)
 * Last activity: [20 years, 5 months ago](https://wordpress.org/support/topic/using-get_posts-before-loop/#post-297818)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
