Advertisement
duck__boy1981

Generate taxonomy filters Class

Apr 22nd, 2015
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class FGW_CPT_Taxonomy_Filter{
  2.    
  3.     /**
  4.      * The custom post type to alter and the taxonomies to show filters for
  5.      *
  6.      * @var array
  7.      */
  8.     private $cpt = array();
  9.    
  10.     /**
  11.      * Constructor
  12.      *
  13.      * @param required array $cpt   The custom post type and associated taxonomies to show filters for (as "array($cpt => array(tax1, tax2))")
  14.      */
  15.     public function __construct($cpt = array()){
  16.    
  17.         $this->cpt = $cpt;
  18.         add_action('restrict_manage_posts', array($this, 'render_filter'));
  19.        
  20.     }
  21.    
  22.     /**
  23.      * Render the taxonomy filter
  24.      */
  25.     public function render_filter(){
  26.    
  27.         global $typenow;
  28.        
  29.         $types = array_keys($this->cpt);        // Grab the post types that are being edited
  30.        
  31.         if(in_array($typenow, $types)):         // Check to see if the page now is one of the custom post types supplied by the user
  32.        
  33.             $filters = $this->cpt[$typenow];    // Grab all of the taxonomies that shold have a filter
  34.            
  35.             foreach($filters as $tax_slug) :
  36.            
  37.                 $tax_obj = get_taxonomy($tax_slug); // Grab the taxonomy object (from the taxonomy slug)
  38.                
  39.                 $dropdown_args = array(             // Output the filter dropdown
  40.                     'show_option_all' =>  $tax_obj->labels->all_items,
  41.                     'taxonomy'      =>  $tax_slug,
  42.                     'name'          =>  $tax_slug,
  43.                     'orderby'        =>  'name',
  44.                     'selected'      =>  (isset($_GET[$tax_slug]) ? $_GET[strtolower($tax_slug)] : null),
  45.                     'hierarchical'  =>  true,
  46.                     'depth'        =>  0,
  47.                     'show_count'      =>  false,
  48.                     'hide_empty'      =>  false
  49.                 );
  50.                 wp_dropdown_categories($dropdown_args);
  51.                
  52.             endforeach;
  53.            
  54.         endif;
  55.        
  56.     }
  57.    
  58. }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement