WordPress.org

Ready to get started?Download WordPress

Forums

Passing variables set by widgets (3 posts)

  1. uxking
    Member
    Posted 4 years ago #

    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;
    	}
  2. uxking
    Member
    Posted 4 years ago #

    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.

  3. mdineshkumarcs
    Member
    Posted 4 years ago #

    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....

Topic Closed

This topic has been closed to new replies.

About this Topic