Title: get_posts and wp_query
Last modified: August 19, 2016

---

# get_posts and wp_query

 *  Resolved [hancoha](https://wordpress.org/support/users/hancoha/)
 * (@hancoha)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/)
 * I’ve been trying to display a list of posts from specific categories and the 
   codex seems easy enough. I’ve tried everything but I can’t seem to get the get_posts
   or query_posts to work in 2.7. I did a search and noticed some people had a problem
   with using certain parameters in the query. I can’t even seem to get anything
   out of either of these functions. Is this a problem with 2.7? Or am I somehow
   messing up the easiest functions?

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

 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052334)
 * Hi – it works fine in 2.7 You are perhaps messing it up, but it is not at all
   the easiest of functions. Please post the segment of your code that is the query_posts
   statement and the beginning of your loop code.
 *  Thread Starter [hancoha](https://wordpress.org/support/users/hancoha/)
 * (@hancoha)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052339)
 * I feel like a total idiot. I figured out how to do the query_posts function. 
   Here is my code:
 *     ```
       <?php $category = get_posts('numberposts=5&amp;category=4'); foreach($category as $post): ?>
       <ul>
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       </ul>
       <?php endforeach; ?>
       ```
   
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052340)
 * Hi
 * Just wanting to point out if you figured it out you can’t be a total idiot since
   this forum is full of people who can’t figure it out and you did!
 *  Thread Starter [hancoha](https://wordpress.org/support/users/hancoha/)
 * (@hancoha)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052342)
 * I didn’t realize you had to iterate using the foreach loop. If you simply do 
   <?php query_posts(); ?> should you not get all the post data?
 *  [stvwlf](https://wordpress.org/support/users/stvwlf/)
 * (@stvwlf)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052344)
 * Hi
 * > If you simply do <?php query_posts(); ?> should you not get all the post data?
 * No. That is a common misunderstanding. What query_posts does is convert the query
   parameters you supply into a database query, run the query, retrieve the query
   output from the database, and convert that output into PHP data structures. You
   then have to do something WITH the retrieved data. That is what the loop does.
 *  Thread Starter [hancoha](https://wordpress.org/support/users/hancoha/)
 * (@hancoha)
 * [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052518)
 * That makes a lot more sense. I understand that it has to obviously query each
   post, if there are any, in order for it to return any data. I really appreciate
   your help.

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

The topic ‘get_posts and wp_query’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 6 replies
 * 2 participants
 * Last reply from: [hancoha](https://wordpress.org/support/users/hancoha/)
 * Last activity: [17 years, 1 month ago](https://wordpress.org/support/topic/get_posts-and-wp_query/#post-1052518)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
