Title: get_posts not working properly
Last modified: August 21, 2016

---

# get_posts not working properly

 *  Resolved [multiformeingegno](https://wordpress.org/support/users/lorenzone92/)
 * (@lorenzone92)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/get_posts-not-working-properly/)
 * This snippet inside a PHP code widget returns 7 links to the same post.
    Outside
   the widget (directly executed for example in a custom template), works properly
   and returns 7 different posts:
 *     ```
       <ul>
       <?php
       $args = array( 'posts_per_page' => 7, 'category' => 9 );
       $tag_posts = get_posts( $args ); foreach ( $tag_posts as $post ) : setup_postdata( $post ); ?>
       	<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
       <?php endforeach; wp_reset_postdata(); ?>
       </ul>
       ```
   
 * [http://wordpress.org/plugins/php-code-widget/](http://wordpress.org/plugins/php-code-widget/)

Viewing 1 replies (of 1 total)

 *  Plugin Author [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [12 years, 4 months ago](https://wordpress.org/support/topic/get_posts-not-working-properly/#post-4370208)
 * You don’t define $post as the global $post before attempting that code.
 * Please read the FAQ and learn how PHP global and local variables work:
 * [http://wordpress.org/plugins/php-code-widget/faq/](http://wordpress.org/plugins/php-code-widget/faq/)

Viewing 1 replies (of 1 total)

The topic ‘get_posts not working properly’ is closed to new replies.

 * ![](https://ps.w.org/php-code-widget/assets/icon.svg?rev=992095)
 * [PHP Code Widget](https://wordpress.org/plugins/php-code-widget/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/php-code-widget/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/php-code-widget/)
 * [Active Topics](https://wordpress.org/support/plugin/php-code-widget/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/php-code-widget/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/php-code-widget/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * Last activity: [12 years, 4 months ago](https://wordpress.org/support/topic/get_posts-not-working-properly/#post-4370208)
 * Status: resolved