Pippins Plugins
  • Email
  • Facebook
  • Feedburner
  • Github
  • Google
  • Twitter
  • Vimeo
  • Youtube
  • Rss
  • About
  • News
  • Join the Site
    • Member Benefits
    • Member Plugins
    • Email Notifications
  • Plugin Store
    • Affiliate Area
    • Checkout
  • Plugins
    • Plugin Portfolio
      • Plugin Portfolio – List View
    • Free
    • Premium
    • Member Plugins
    • Coding Standards
    • Get Plugin Support
  • Tutorials
    • Series
      • Plugin Development 101
      • Creating a User Follow System Plugin
      • Customizing Restrict Content Pro
      • Displaying Content with Easy Content Types
      • Writing Your First WordPress Plugins, Basic to Advanced
      • Working with Widgets
      • User Submitted Image Galleries
      • Plugin Thoughts
      • Integrating Stripe.com with WordPress
      • WordPress Rewrite API
    • Member Exclusive
      • Free Members
      • Subscriber Only
    • Difficulty
      • Beginner
      • Intermediate
      • Advanced
    • Action and Filter Hooks
    • Ajax
    • Custom Post Types
    • External APIs
    • Short Codes
    • Taxonomies
    • Video Tutorials
    • Widget Tutorials
    • WordPress Admin / Dashboard
    • Working with jQuery
    • WordPress Database
    • Writing Plugins
    • Tag Index
  • Reviews
  • Support Forum
  • Contact
    • Support the Site
    • Request Code Review
    • Plugin Support

Filter Posts by Custom Taxonomy in Admin

Posted on November 18, 2011 by Pippin in Advanced, Custom Post Types, Quick Tips, Taxonomies, Tutorials, WordPress Admin / Dashboard 7 Comments
Home» Tutorials » Advanced » Filter Posts by Custom Taxonomy in Admin
Tweet
Love It - 1

If you have lots of posts in your WordPress site, you have probably used the Category and/or Tag filters at the top of the post list page. These filters are great because they very quickly allow you to limit the kind of posts that are displayed, and let you find the one(s) you are looking for with relative ease. Well, if you use any custom post types or custom taxonomies, then you have probably noticed that these options are not available. So I’m going to show you how to add new filters for your custom taxonomies to any custom post type you have registered on your site.

The process is pretty simple, in that it does not require a lot of code, and WordPress does most of the work for you, but the function itself is a little bit complex. What we are going to do is setup a function that creates a drop down select menu based off of the terms in the taxonomies we provide. So if we set the function to display a filter for the “genres” taxonomy, it will retrieve a list of all of the terms in that taxonomy (all terms that are not empty), and then display each of those terms as an option in the drop down menu. If we provide multiple taxonomies, through an array, then the function will create a drop down for each set of taxonomy terms.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
function pippin_add_taxonomy_filters() {
	global $typenow;
 
	// an array of all the taxonomyies you want to display. Use the taxonomy name or slug
	$taxonomies = array('faq_topics');
 
	// must set this to the post type you want the filter(s) displayed on
	if( $typenow == 'faqs' ){
 
		foreach ($taxonomies as $tax_slug) {
			$tax_obj = get_taxonomy($tax_slug);
			$tax_name = $tax_obj->labels->name;
			$terms = get_terms($tax_slug);
			if(count($terms) > 0) {
				echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
				echo "<option value=''>Show All $tax_name</option>";
				foreach ($terms as $term) { 
					echo '<option value='. $term->slug, $_GET[$tax_slug] == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>'; 
				}
				echo "</select>";
			}
		}
	}
}
add_action( 'restrict_manage_posts', 'pippin_add_taxonomy_filters' );

The function is then passed through the “restrict_manage_posts” action hook in order for the select menus to be displayed in the post list page.

If everything worked correctly, you should see something like this:

Note that in the function there is a check for the current post type. If you do not set the conditional $typenow statement to the post type you want the filter displayed on, it will show on ALL post type pages, and since not all of your post types may have the custom taxonomy, this is not generally good idea.

If you are a user of my Easy Content Types plugin, then you will see these taxonomy filters coming very soon with an add-on plugin.

Tweet Follow @pippinsplugins
drop down, filter, post types, taxonomy

7 comments on “Filter Posts by Custom Taxonomy in Admin”

  1. Bruce says:
    November 18, 2011 at 6:10 pm

    Very nice. Thanks for the great tip. I had some code that struggled to do this, but never seemed to work out…

    This was one piece of it:

    // ***Add Filter for CPTs by Custom Taxonomy***
    // Filter the request to just give posts for the given taxonomy, if applicable.
    // add ‘taxonomies’ => ‘category’ // Add tags and categories taxonomies – To CPT register args to limit specific filter
    function taxonomy_filter_restrict_manage_posts() {
    global $typenow;
    if ( is_singular( ‘we_books’ ) )
    return;
    // If you only want this to work for your specific post type,
    // check for that $type here and then return.
    // This function, if unmodified, will add the dropdown for each
    // post type / taxonomy combination.

    $post_types = get_post_types( array( ‘_builtin’ => false ) );

    if ( in_array( $typenow, $post_types ) ) {
    $filters = get_object_taxonomies( $typenow );

    foreach ( $filters as $tax_slug ) {
    $tax_obj = get_taxonomy( $tax_slug );
    wp_dropdown_categories( array(
    ‘show_option_all’ => __(‘Show All ‘.$tax_obj->label ),
    ‘taxonomy’ => $tax_slug,
    ‘name’ => $tax_obj->name,
    ‘orderby’ => ‘name’,
    ‘selected’ => $_GET[$tax_slug],
    ‘hierarchical’ => $tax_obj->hierarchical,
    ‘show_count’ => false,
    ‘hide_empty’ => true
    ) );
    }
    }
    }

    add_action( ‘restrict_manage_posts’, ‘taxonomy_filter_restrict_manage_posts’ );

    There was additional code, much more than you have there… I never got a grip on understanding it.
    Yours works great!

    Reply
    • Pippin says:
      November 18, 2011 at 8:33 pm

      Yeah, I can see a couple of problems with that code. Glad mine works for you :)

  2. RegisIT says:
    June 20, 2012 at 7:20 am

    Once you have selected a taxonomy filter you cannot select another without first going back to the full custom post type listing – otherwise gives “invalid post type”.

    Does the paid-for plug-in fix this issue?

    Reply
    • Pippin says:
      June 20, 2012 at 11:57 am

      By paid plugin, do you mean Easy Content Types and the ECPT Filter by Taxonomy add-on?

  3. Devin Price says:
    October 3, 2012 at 5:16 pm

    Hi Pippin. Thanks for posting this code, I just added it to my Portfolio Post Type plugin: https://github.com/devinsays/portfolio-post-type/commit/6664a1dff46257187689ed3aa8ca6f7447c04458

    I did notice one issue. $_GET[$tax_slug] was throwing an isset notice when you weren’t in the taxonomy view. I fixed this by checking for it explicitly:


    $current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;

    Reply
    • Pippin says:
      October 4, 2012 at 2:37 pm

      Oh yeah, the isset() should definitely be there. Thanks for noticing.

  4. Alicia St Rose says:
    February 6, 2013 at 10:21 am

    Beautiful! You make me look like a rockstar! Thanks for that awesome snippet!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • Login

Lost your password?

Please enter your username or e-mail address. You will receive a new password via e-mail.

  • Facebook Become a Fan Like

  • Twitter Subscribe on Twitter Follow

  • YouTube Follow my Videos Subscribe

  • RSS Feed Subscribe with RSS Subscribe

Easy Digital Downloads

Most Loved

  • Love It Pro for WordPress
  • Write a “Love It” Plugin with Ajax to Let Users Love Their Favorite Posts / Pages
  • Simple Notices Pro Plugin for WordPress
  • User Bookmarks for WordPress
  • Front End Registration and Login Forms Plugin

Similar Plugins and Posts

  • Using Filters to Alter HTML Output
  • A Quick Introduction to Using Filters
  • Listing Custom Post Type Entries with a Short Code
  • Sugar Modal Windows Plugin
  • Better Recent Posts Widget Pro

Latest Premium Content

  • Plugin Development 101 – Introduction to Adding Dashboard Menus
  • Plugin Development 101 – Intro to Loading Scripts and Styles
  • User Follow System – Part 5
  • Plugin Development 101 – Intro to Short Codes

Latest Tutorials

  • Storing Session Data in WordPress without $_SESSION (19)

    The term Session in web development refers to...

  • Test Your Plugins with RTL (1)

    Right-To-Left languages are those that...

  • Submitting Your First Pull Request to a WordPress Plugin on Github (5)

    Github is an extremely popular tool for managing WordPress plugins, and one...

Enter your email to receive automated updates when new posts are published

WP Core Contributions

  • [24316]

View the ticket on Trac.

WP Codex Contributions

  • Function: shortcode exists
  • Function: has shortcode
  • Function: shortcode exists
  • Function: shortcode exists
  • Function: has shortcode

View all 41 changes in the Codex.

Latest Tweets

  • Could not fetch Twitter RSS feed.

Topics

contextual help Sugar Event Calendar wp_enqueue_script hook campaign monitor Tom McFarlin shortcodes featured Rémi Corson the_content register_setting add_options_page attachments forms short codes Related posts plugin authors do_action attachment mail chimp image login recent posts apply_filters post types comments bbpress short code taxonomies custom post type gallery Ajax images Stripe taxonomy jquery users widgets add_filter easy content types add_action widget restrict content pro easy digital downloads

Weekly Newsletter

Useful Links

  • Join the Site
  • Plugin Store
  • Affiliate Area
  • Tag Index
  • Support the Site
  • Suggest a Tutorial
  • Random Post
  • Contact

Monthly Archives

(c) 2013 Pippin's Plugins