Title: Pass widget variable to external function
Last modified: August 21, 2016

---

# Pass widget variable to external function

 *  [segan93](https://wordpress.org/support/users/segan93/)
 * (@segan93)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/pass-widget-variable-to-external-function/)
 * Not sure if this should be moved to WP-Advanced or not, but here it goes.
 * I am working on a custom widget which displays posts from a selected taxonomy(
   category or tag). This widget contains a textbox which the user enters an ID 
   of a taxonomy (category or tag) to exclude from posts. This ID can vary from 
   widget to widget as multiple instances of this widget will appear on the same
   page. **I am looking for a way to pass the `$exclude` variable from `widget()`
   to the `excludeTheID()`function which is located in functions.php**
 * For the sake of this example, here is a mock of what I am trying to accomplish:
 *     ```
       class testWidget extends WP_Widget {
   
           public function __construct() {
               // .......
           }
   
           public function form( $instance ) {
               // This widget contains a text field ($exclude)  that the user is to enter a taxonomy ID into.
           }
   
           public function update( $new_instance, $old_instance ) {
               // .......
           }
   
           public function widget( $args, $instance ) {
               // this value represents what the user would enter in the textbox of the widget
               $exclude = 15;
               // .......
   
               // This function does the work
               add_filter( "posts_where", "testFunction" );    
   
               // Create a new query
               $loop = new WP_Query();
   
               // Remove the filter
               remove_filter( "posts_where", "testFunction" );
           }
       }
   
       // In Functions.php: External function responsible for generating the new WHERE clause using the  value in $exclude.
   
       function excludeTheID( $where, $exclude )
       {   
   
           $clauses = array(
               array(
                   'taxonomy'  => 'category',
                   'field'     => 'id',
                   'terms'     => $exclude,
                   'operator' => 'NOT IN',
               ),
           );
   
           // Access the global WordPress DB variable
           global $wpdb;
   
           // Create the new WHERE Query string using the above arrays
           $tax_sql = get_tax_sql( $clauses, $wpdb->posts, 'ID' );
           $where .= $tax_sql['where'];
   
           return $where;
       }
       ```
   
 * I do not want to use globals to accomplish this as each widget will be passing
   a unique value for $exclude in every instance of that widget on a page.
 * Any guidance would be greatly appreciated 🙂
 * Thanks.

Viewing 1 replies (of 1 total)

 *  Thread Starter [segan93](https://wordpress.org/support/users/segan93/)
 * (@segan93)
 * [12 years, 2 months ago](https://wordpress.org/support/topic/pass-widget-variable-to-external-function/#post-4783458)
 * **Edit: the above filter has a typo. `testFunction()` should be `excludeTheID()`**

Viewing 1 replies (of 1 total)

The topic ‘Pass widget variable to external function’ is closed to new replies.

## Tags

 * [instance](https://wordpress.org/support/topic-tag/instance/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * 1 reply
 * 1 participant
 * Last reply from: [segan93](https://wordpress.org/support/users/segan93/)
 * Last activity: [12 years, 2 months ago](https://wordpress.org/support/topic/pass-widget-variable-to-external-function/#post-4783458)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
