Title: Get_posts example from DOCS not working.
Last modified: August 18, 2016

---

# Get_posts example from DOCS not working.

 *  [evergreen](https://wordpress.org/support/users/evergreen/)
 * (@evergreen)
 * [19 years, 11 months ago](https://wordpress.org/support/topic/get_posts-example-from-docs-not-working/)
 * Hi all,
 * I pasted the example of get_posts from the support docs into my site and I am
   getting the following error:
 * “invalid argument supplied for foreach()”
 * I am using the latest version of WP and here is the code:
 * `
    <ul> <?php $posts = get_posts('numberposts=5&offset=1&category=1'); foreach(
   $posts as $post) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title();?
   ></a> --- <?php the_excerpt(); ?></li> <?php endforeach; ?> </ul>

Viewing 1 replies (of 1 total)

 *  [hussv1](https://wordpress.org/support/users/hussv1/)
 * (@hussv1)
 * [19 years, 2 months ago](https://wordpress.org/support/topic/get_posts-example-from-docs-not-working/#post-403126)
 * I got that error when my query doest return any results.
    You need to check for
   that first, like this: <?php $posts = get_posts(‘numberposts=5&offset=1&category
   =1’); if ($posts!=”) { foreach($posts as $post) : ?>
    -  — <?php the_excerpt(); ?>
    - <?php endforeach; ?>
       <?}?>

Viewing 1 replies (of 1 total)

The topic ‘Get_posts example from DOCS not working.’ is closed to new replies.

## Tags

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

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 1 reply
 * 2 participants
 * Last reply from: [hussv1](https://wordpress.org/support/users/hussv1/)
 * Last activity: [19 years, 2 months ago](https://wordpress.org/support/topic/get_posts-example-from-docs-not-working/#post-403126)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
