Title: WP_Query loop inside widget error
Last modified: August 20, 2016

---

# WP_Query loop inside widget error

 *  Resolved [Driftless](https://wordpress.org/support/users/driftless1/)
 * (@driftless1)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_query-loop-inside-wideget/)
 * I’m having issues with a custom wp_query in the widget. This code works fine 
   throughout my site:
 *     ```
       <?php
       $facts = custom_wp_query('3'); //get 3 results
         //echo "<pre>"; print_r($facts); echo "</pre>";
       while ( $facts->have_posts() ) : $facts->the_post();
         echo "<pre>"; print_r($post); echo "</pre>";//This works elsewhere
       endwhile;
       ?>
       ```
   
 * The custom query gives me 3 wp_post objects in $facts as expected. However, the
   while loop fails saying $post is not defined. This works elsewhere on my site,
   but not in this widget. What am I missing?
 * [http://wordpress.org/extend/plugins/php-code-widget/](http://wordpress.org/extend/plugins/php-code-widget/)

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

 *  Thread Starter [Driftless](https://wordpress.org/support/users/driftless1/)
 * (@driftless1)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_query-loop-inside-wideget/#post-3474423)
 * And… it returns the error 3 times, meaning the loop knows there is 3 posts, they
   just aren’t being shifted to the global $post variable…
 *  Thread Starter [Driftless](https://wordpress.org/support/users/driftless1/)
 * (@driftless1)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_query-loop-inside-wideget/#post-3474488)
 * Got it working with:
 *     ```
       <?php
       $facts = custom_wp_query('3'); //get 3 results
       while ( $facts->have_posts() ) : $facts->the_post();
         $post = $facts->post;
         echo "<pre>"; print_r($post); echo "</pre>";//This works elsewhere
       endwhile;
       ?>
       ```
   
 * Strange that the previous works elsewhere…
 *  Plugin Author [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_query-loop-inside-wideget/#post-3474507)
 * You didn’t declare that the $post was global by using a `global $post` call anywhere.
 * The code inside the widget will run in a function context, not in the global 
   context. You need to declare what variables you’re using that are globals.
 *  Thread Starter [Driftless](https://wordpress.org/support/users/driftless1/)
 * (@driftless1)
 * [13 years, 3 months ago](https://wordpress.org/support/topic/wp_query-loop-inside-wideget/#post-3474515)
 * Makes sense, thanks.

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

The topic ‘WP_Query loop inside widget error’ 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/)

## Tags

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

 * 4 replies
 * 2 participants
 * Last reply from: [Driftless](https://wordpress.org/support/users/driftless1/)
 * Last activity: [13 years, 3 months ago](https://wordpress.org/support/topic/wp_query-loop-inside-wideget/#post-3474515)
 * Status: resolved