Title: [Plugin: PHP Code Widget] didnt work
Last modified: August 20, 2016

---

# [Plugin: PHP Code Widget] didnt work

 *  Resolved [Mark Shirley](https://wordpress.org/support/users/ravalde/)
 * (@ravalde)
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-php-code-widget-didnt-work/)
 * Didnt work for me
 * [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)

 *  Plugin Author [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [14 years, 1 month ago](https://wordpress.org/support/topic/plugin-php-code-widget-didnt-work/#post-2703432)
 * Works fine for everybody else.
 *  [flint_and_tinder](https://wordpress.org/support/users/flint_and_tinder/)
 * (@flint_and_tinder)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/plugin-php-code-widget-didnt-work/#post-2703655)
 * What a useful reply that is Samuel.
 * It doesn’t work for me either, I’m trying to use the following code, which works
   fine in the template, but not when I use it in the widget:
 *     ```
       <?php $custom_taxterms = wp_get_object_terms( $post->ID, 'tribe_events_cat', array('fields' => 'ids') );
       $args = array(
       'post_type' => 'tribe_events',
       'post_status' => 'publish',
       'posts_per_page' => 3,
       'orderby' => 'rand',
       'tax_query' => array(
           array(
               'taxonomy' => 'tribe_events_cat',
               'field' => 'id',
               'terms' => $custom_taxterms
           )
       ),
       'post__not_in' => array ($post->ID),
       );
       $related_items = new WP_Query( $args );
       if ($related_items->have_posts()) :
       echo '<div>';
       while ( $related_items->have_posts() ) : $related_items->the_post();
       ?>
           <div>
           	<h4><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
               <h5><meta itemprop="startDate" content="<?php echo tribe_get_start_date( null, false, 'Y-m-d-h:i:s' ); ?>"/><?php echo tribe_get_start_date(); ?></h5>
               <p><?php echo excerpt(20); ?></p>
           </div>
       <?php
       endwhile;
       echo '</div>';
       endif;
       wp_reset_postdata();
       ?>
       ```
   
 *  Plugin Author [Samuel Wood (Otto)](https://wordpress.org/support/users/otto42/)
 * (@otto42)
 * WordPress.org Admin
 * [13 years, 4 months ago](https://wordpress.org/support/topic/plugin-php-code-widget-didnt-work/#post-2703656)
 * If your code doesn’t work in the widget, then something is wrong with your code.
   The widget just runs the code as-is, it doesn’t perform magic tricks.
 * In your case, the “$post” variable is not defined because you failed to declare
   it as a global. Code in templates run in the global variable scope. Code in the
   widget will run inside a function scope, and you need to account for that difference.
 *  [flint_and_tinder](https://wordpress.org/support/users/flint_and_tinder/)
 * (@flint_and_tinder)
 * [13 years, 4 months ago](https://wordpress.org/support/topic/plugin-php-code-widget-didnt-work/#post-2703657)
 * Much better thanks.
 * I appear to have fixed it by adding a simple `global $post;` at the top of the
   loop.

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

The topic ‘[Plugin: PHP Code Widget] didnt work’ 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/)

 * 4 replies
 * 3 participants
 * Last reply from: [flint_and_tinder](https://wordpress.org/support/users/flint_and_tinder/)
 * Last activity: [13 years, 4 months ago](https://wordpress.org/support/topic/plugin-php-code-widget-didnt-work/#post-2703657)
 * Status: resolved