Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class FGW_CPT_Taxonomy_Filter{
- /**
- * The custom post type to alter and the taxonomies to show filters for
- *
- * @var array
- */
- private $cpt = array();
- /**
- * Constructor
- *
- * @param required array $cpt The custom post type and associated taxonomies to show filters for (as "array($cpt => array(tax1, tax2))")
- */
- public function __construct($cpt = array()){
- $this->cpt = $cpt;
- add_action('restrict_manage_posts', array($this, 'render_filter'));
- }
- /**
- * Render the taxonomy filter
- */
- public function render_filter(){
- global $typenow;
- $types = array_keys($this->cpt); // Grab the post types that are being edited
- if(in_array($typenow, $types)): // Check to see if the page now is one of the custom post types supplied by the user
- $filters = $this->cpt[$typenow]; // Grab all of the taxonomies that shold have a filter
- foreach($filters as $tax_slug) :
- $tax_obj = get_taxonomy($tax_slug); // Grab the taxonomy object (from the taxonomy slug)
- $dropdown_args = array( // Output the filter dropdown
- 'show_option_all' => $tax_obj->labels->all_items,
- 'taxonomy' => $tax_slug,
- 'name' => $tax_slug,
- 'orderby' => 'name',
- 'selected' => (isset($_GET[$tax_slug]) ? $_GET[strtolower($tax_slug)] : null),
- 'hierarchical' => true,
- 'depth' => 0,
- 'show_count' => false,
- 'hide_empty' => false
- );
- wp_dropdown_categories($dropdown_args);
- endforeach;
- endif;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement