ctp

Posted by & filed under Tips and Tricks, wordpress, Wordpress Themes.

Please note that “this post is for them who knows custom post types and custom taxonomy in wordpress”

Isn’t the title enough to get what my article about ?

Let me paste the code what I did: I think I collected the basic code from somewhere and then edited as I need.

add_action( 'restrict_manage_posts', 'todo_restrict_manage_posts' );
add_filter('parse_query','todo_convert_restrict');
function todo_restrict_manage_posts() {
    global $typenow;
    //var_dump($typenow);
    $args   =   array( 'public' => true, '_builtin' => false );
    $post_types = get_post_types($args);
    //var_dump($post_types);
    //$post_types = array('blogs', 'product', 'portfolios');
    //var_dump($post_types);
    if ( in_array($typenow, $post_types) ) {
        $filter = get_object_taxonomies($typenow);
        /*
        $filters    = array(
                        'blogs'          => array('blog_cat','blog_tag'),
                        'product'       => array('product_tag','product_cat'),
                        'portfolios'    => array('technology')
        );
        $filter = $filters[$typenow];
        */

        //var_dump($filters);
        foreach ($filter as $tax_slug) {
            //foreach($tax_slug 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_obj->query_var],
                    'hierarchical' => $tax_obj->hierarchical,
                    'show_count' => true,
                    'hide_empty' => false
                ));
            //}
        }
    }
}
function todo_convert_restrict($query) {
    global $pagenow;
    global $typenow;
    if ($pagenow=='edit.php') {
        $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $var = &$query->query_vars[$tax_slug];
            if ( isset($var) ) {
                $term = get_term_by('id',$var,$tax_slug);
                $var = $term->slug;
            }
        }
    }
}
function override_is_tax_on_post_search($query) {
    global $pagenow;
    $qv = &$query->query_vars;
    if ($pagenow == 'edit.php' && isset($qv['taxonomy']) && isset($qv['s'])) {
        $query->is_tax = true;
    }
}
add_filter('parse_query','override_is_tax_on_post_search');

What the code does:

1. The hook/action is executed when admin post listing is done
2. It’s finds all the custom post types and if post type and post listing type match it finds the custom taxonomy types for that post type.
3. Shows the Custom taxonomy types for custom post type as filter or drop down list
4. Here we did a check if the search query is same the taxonomy!
5. That’s it.

Let me show a screenshot how it looks for my custom post type named ‘product’ and it’s custom taxonomy as category and tags.

Update: I have a pro plugin to do this. Any one interested can buy  from my company sites from here download.

9 Responses to “Showing custom taxonomy filter in custom post types in wordpress admin post listing”

  1. Jason

    I am getting an error:
    Undefined index: tag
    Undefined index: category

    Its being reported here: ‘selected’ => $_GET[$tax_obj->query_var],

    The script seems to work, they error shows with debug on.

  2. Kassandra

    Wow this is exactly what I was looking for! Thank you!

  3. Alin

    Hi Manchumahara,
    I created a taxonomie with this with paste in functions.php:

    add_action( ‘init’, ‘create_protocols_taxonomies’, 0 );
    function create_protocols_taxonomies() { register_taxonomy(‘protocols’,’post’, array(…)); }

    All I want now is besides Name, Tags, Category, Date, to be Protocols. I just can’t figure it out.

    Thanks

  4. Delus

    Thanks! I not a php developer.. I have been searching high and low for a solution… your code doesn’t even need to type in the “Custom Post Type” name and it works perfectly!!

    COPY & PASTE! =)

    • Manchumahara(Sabuj Kundu)

      @Delus , you are right, I just the thing easy for me and made easy for others to copy & paste. May be I will convert it as a plugin for more customization option for those who can not copy paste but can add plugin !

Leave a Reply

  • (will not be published)