Skip to content

Instantly share code, notes, and snippets.

@cfxd
Forked from bainternet/tax_cpt_filter_class.php
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfxd/89066eda06457da0f366 to your computer and use it in GitHub Desktop.
Save cfxd/89066eda06457da0f366 to your computer and use it in GitHub Desktop.
<?php
if (!class_exists('Tax_CTP_Filter')){
/**
* Tax CTP Filter Class
* Simple class to add custom taxonomy dropdown to a custom post type admin edit list
* @author Ohad Raz <admin@bainternet.info>
* @version 0.1
*/
class Tax_CTP_Filter
{
/**
* __construct
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @param array $cpt [description]
*/
function __construct($cpt = array()){
$this->cpt = $cpt;
// Adding a Taxonomy Filter to Admin List for a Custom Post Type
add_action( 'restrict_manage_posts', array($this,'my_restrict_manage_posts' ));
}
/**
* my_restrict_manage_posts add the slelect dropdown per taxonomy
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @return void
*/
public function my_restrict_manage_posts() {
// only display these taxonomy filters on desired custom post_type listings
global $typenow;
$types = array_keys($this->cpt);
if (in_array($typenow, $types)) {
// create an array of taxonomy slugs you want to filter by - if you want to retrieve all taxonomies, could use get_taxonomies() to build the list
$filters = $this->cpt[$typenow];
foreach ($filters as $tax_slug) {
// retrieve the taxonomy object
$tax_obj = get_taxonomy($tax_slug);
$tax_name = $tax_obj->labels->name;
// output html for taxonomy dropdown filter
echo "<select name='".strtolower($tax_slug)."' id='".strtolower($tax_slug)."' class='postform'>";
echo "<option value=''>Show All $tax_name</option>";
$this->generate_taxonomy_options($tax_slug,0,0,(isset($_GET[strtolower($tax_slug)])? $_GET[strtolower($tax_slug)] : null));
echo "</select>";
}
}
}
/**
* generate_taxonomy_options generate dropdown
* @author Ohad Raz <admin@bainternet.info>
* @since 0.1
* @param string $tax_slug
* @param string $parent
* @param integer $level
* @param string $selected
* @return void
*/
public function generate_taxonomy_options($tax_slug, $parent = '', $level = 0,$selected = null) {
$args = array('show_empty' => 1);
$show_empty = $args['show_empty'] == 0 ? false : true;
if(!is_null($parent)) {
$args = array('parent' => $parent);
}
$terms = get_terms($tax_slug,$args);
$tab='';
for($i=0;$i<$level;$i++){
$tab.='--';
}
foreach ($terms as $term) {
// get term count for current post type
global $typenow;
$tax_posts = get_posts(array(
'fields' =>'ids',
'posts_per_page' => -1,
'post_type' => $typenow,
'tax_query' => array(array(
'taxonomy' => $tax_slug,
'field' => 'slug',
'terms' => $term->slug
))
));
if($show_empty || (!$show_empty && count($tax_posts) > 0)) {
// output each select option line, check against the last $_GET to show the current option selected
echo '<option value='. $term->slug, $selected == $term->slug ? ' selected="selected"' : '','>' .$tab. $term->name .' (' . count($tax_posts) . ')</option>';
$this->generate_taxonomy_options($tax_slug, $term->term_id, $level+1,$selected);
}
}
}
}//end class
}//end if
/*
//usage:
new Tax_CTP_Filter(array(
'CUSTOM_POST_TYPE_NAME' => array('CUSTOM_TAXONOMY_NAME2','CUSTOM_TAXONOMY_NAME2')
)
);
*/
@cfxd
Copy link
Author

cfxd commented May 23, 2014

Edited this so that each taxonomy term count reflects only the count of the current post type and also to ensure the show_empty parameter is honored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment