Adding Taxonomy Filter to Admin posts List for Custom Post Type

terminquery

在 wordpress 默认的文章

class TerminQuery{
	public function __construct() {
		add_filter('parse_query',array($this, 'query'));
		add_action('restrict_manage_posts',array($this, 'restrict'));
	}

	function query($query) {
		global $pagenow;
		global $typenow;
		$qv = &$query->query_vars;
		if ($typenow!='post' && $pagenow=='edit.php') {
			$taxonomies = get_object_taxonomies($typenow);
			foreach ($taxonomies as $taxonomy) {
				if (isset($qv[$taxonomy]) && is_numeric($qv[$taxonomy])) {
					$term = get_term_by('id',$qv[$taxonomy],$taxonomy);
					$qv[$taxonomy] = ($term ? $term->slug : '');
				}
			}
		}
	}

	function restrict() {
		global $typenow;
		global $wp_query;
		//var_dump($typenow);
		if ($typenow!='post') {
			$taxonomies = get_object_taxonomies($typenow);
			foreach ($taxonomies as $taxonomy) {
				$term = get_taxonomy( $taxonomy );
				//print_r($term);
				wp_dropdown_categories(array(
		            'show_option_all' =>  $term->label,
		            'taxonomy'        =>  $taxonomy,
		            'name'            =>  $term->name,
		            'class'			  =>  $term->name,
		            'orderby'         =>  'name',
		            'selected'        =>  isset($wp_query->query[$term->name]) ? $wp_query->query[$term->name] :'',
		            'hierarchical'    =>  true,
		            'depth'           =>  -1,
		            'show_count'      =>  true, // Show # listings in parens
		            'hide_empty'      =>  true, // Don't show businesses w/o listings
				));
			}
		}
	}
}

new TerminQuery();

 

Comments are closed.