I have a plugin that creates a widget where the user selects a category for display. How can I pass that category they chose to the add_filter() function. Or maybe use the chosen category in the function that I call with the add_filter? (see comment line in CAPS in below code)
add_filter('the_posts', 'post_exclusion', 10);
function post_exclusion( $posts ) {
if ( is_admin() ) { return $posts; }
//create an array to hold the posts we want to show
$new_posts = array();
//loop through all the post objects
foreach( $posts as $post ) {
//for each object get an array of applicable categories
$cats = get_the_category( $post->ID );
//create a variable that determins if this post should be included
//the default is true, i.e. include the post
$include = true;
//loop through all the categories applicable to the post
foreach( $cats as $cat) {
//if the post is assigned to our undesirable category then do not include it
//I WANT TO USE THE CHOSEN CATEGORY FROM THE WIDGET HERE
if ( $cat->name == 'Testimonials' || $cat->name == 'Listsonly' ) { $include = false; }
}
//if we want to include it then add the post object to the new posts array
if ( $include === true ) { $new_posts[] = $post; }
}
//send the new post array back to be used by WordPress
return $new_posts;
}