Title: my php code works and it doesn&#039;t
Last modified: August 22, 2016

---

# my php code works and it doesn't

 *  Resolved [Mikael Boldt](https://wordpress.org/support/users/boldt/)
 * (@boldt)
 * [11 years, 6 months ago](https://wordpress.org/support/topic/my-php-code-works-and-it-doesnt/)
 * I am upgrading an old WP page. and a part of it is to introduce widgets.
    Some
   of my previous code I want to present in your widget in order to simplify possible
   changes in a child-theme.
 * I have this piece of code:
 *     ```
       <?php $args = array(
       	'posts_per_page'  => 20,
       	'offset'          => 0,
       	'category'        => 11,
       	'orderby'         => 'post_date',
       	'order'           => 'DESC',
       	'include'         => '',
       	'exclude'         => '',
       	'meta_key'        => '',
       	'meta_value'      => '',
       	'post_type'       => 'post',
       	'post_mime_type'  => '',
       	'post_parent'     => '',
       	'post_status'     => 'publish',
       	'suppress_filters' => true );
   
       $postslist = get_posts( $args );
       foreach ($postslist as $post) :  setup_postdata($post); ?>
       	<div>
       		<a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) { the_post_thumbnail(thumbnail); } ?>
       	</br>
       		<?php the_title(); ?></a>
       	</br></br>
       	</div>
       <?php endforeach; ?>
       ```
   
 * it is working in my old theme, showing 20 posts thumbs and headlines. I am using
   it to present my category “photo album” in sidebar.
 * the code is working, when it is hard-coded into sidebar.php, but when it is placed
   in your widget plugin – it doesn’t (it just shows the same post 20-times)
 * What is wrong?
 * [https://wordpress.org/plugins/php-code-widget/](https://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
 * [11 years, 6 months ago](https://wordpress.org/support/topic/my-php-code-works-and-it-doesnt/#post-5404594)
 * Probably because your code is assuming the global $post, but no real way to tell.
   Try adding a `global $post` to the top of it.

Viewing 1 replies (of 1 total)

The topic ‘my php code works and it doesn't’ 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: [11 years, 6 months ago](https://wordpress.org/support/topic/my-php-code-works-and-it-doesnt/#post-5404594)
 * Status: resolved