Pass widget variable to external function

Firstly, you will need your code to store the exclude value somewhere.

Since you don’t want to use a global ( rightly so ), you have few remaining options:

  • a class
  • the class you already have
  • a closure
  • standard APIs

Option 1 a new object/class

Here we create an object, that holds the category you’re excluding, and some logic to exclude it.

class wpse140557_exclude {
    $exclude = 0;
    public __construct( $exclude ) {
        $this->exclude = $exclude;
        add_filter( 'posts_where', array( $this, 'exclude_filter' ) );
    }
    public function exclude_filter( ... ) {
        // etc... using $this->exclude
    }
     public function remove_filter() {
        remove_filter( 'posts_where', array( $this, 'exclude_filter' ) );
     }
}

$exclude = new wpse140557_exclude( 12 ); // excluding category 12

// do some stuff/loops

$exclude->remove_filter(); // remove our filter

Option 2 closures

Here we use a closure, these require PHP 5.3+ to use

$exclude_closure = function (.. args...) use ( $exclude ) {
    // exclusion code $exclude
}

add_filter( 'posts_where', $exclude_closure );

// do stuff

remove_filter('posts_where', $exclude_closure );

Option 3, the class you already have

Move the function into your widget class, then use:

add_filter( "posts_where", array( $this, "excludeTheID" ) );

// do stuff

remove_filter( "posts_where", array( $this, "excludeTheID" ) );

Then use $this->exclude to access/set your category to exclude. This works as a less generic version of Option 1.

Option 4, WP_Query

If you had looked at the official documentation, you would have found there is a section titled “Exclude Posts Belonging to Category”

This shows you 2 ways of doing what you want without needing your additional function:

$query = new WP_Query( 'cat=-12,-34,-56' );

and

$query = new WP_Query( array( 'category__not_in' => array( 2, 6 ) ) );

Similar parameters exist for tags and other taxonomies

Homework

I suggest you read up on the following: