• 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;
    	}
Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter uxking

    (@uxking)

    Should I be using the update_option() to save the widget settings for later? I’m wondering if adding fields to the options table is not a desired practice.

    I am using select box to list the category names in my widget.For instance ,select box list 3 categories.I have added 2 widget in my sidebar.When i click widget edit in admin page it displays the categories as in order.I want select box to display category name as selected first what i chosen for that widget . Please help me to fix this….

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Passing variables set by widgets’ is closed to new replies.