Enhancing WordPress Dashboard: Adding Custom Filters for Custom Post Types

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInPrint this page

WordPress provides a powerful and flexible platform for building websites, and its dashboard is a central hub for managing content. If you have custom post types in your WordPress project, you may want to enhance the user experience by adding custom filters to streamline the content management process. In this article, we’ll explore how to programmatically add a drop-down filter for a custom post type in the WordPress dashboard.

Understanding Custom Post Types and Taxonomies

Before diving into the code, let’s have a quick overview of custom post types and taxonomies in WordPress:

  • Custom Post Types
    Custom post types allow you to define different types of content beyond the default posts and pages. For example, you might have a custom post type called ‘Feature’ to highlight special content on your website.
  • Taxonomies
    Taxonomies are a way to organize and categorize content. They can be hierarchical, like categories, or non-hierarchical, like tags. In our case, we’ll focus on a custom non-hierarchical taxonomy called ‘feature_category’ to categorize our ‘Feature’ custom post type.
  • The Need for Custom Filters
    As your content grows, managing and finding specific items within your custom post types can become challenging. Custom filters provide a convenient way to narrow down the displayed content based on specific criteria.

Adding a Custom Drop-Down Filter

To implement a drop-down filter for a custom post type, we’ll leverage two key WordPress hooks: restrict_manage_posts and parse_query. The restrict_manage_posts hook allows us to add HTML elements to the admin post list filter area, and the parse_query hook enables us to modify the query based on the selected filter.

Let’s break down the process into steps:

Step 1: Identify the Custom Post Type

We start by identifying the custom post type for which we want to add the drop-down filter. In our example, we’ll use the ‘Feature’ custom post type.

Step 2: Get Taxonomy Terms

We retrieve the terms from the custom taxonomy (‘feature_category’) associated with our custom post type. This step ensures that the drop-down filter contains the relevant categories.

Step 3: Display the Drop-Down Filter

Using the wp_dropdown_categories function, we create the HTML for the drop-down filter. This function simplifies the process of generating a taxonomy-specific dropdown list.

Step 4: Filter the Query

We use the parse_query hook to modify the query based on the selected category. This ensures that only posts belonging to the selected category are displayed.

Putting It All Together

Combining these steps results in a seamless integration of a drop-down filter for our custom post type in the WordPress dashboard. Users can now easily filter and locate specific content within the ‘Feature’ post type.

Practical Implementation

Now, let’s dive into the practical implementation. The following code snippets should be added to your theme’s functions.php file or a custom plugin or a custom file on /wp-content/mu-plugins/. The custom post type and the taxonomy can be changed at the top of the code, to accommodate the actual types.

<?php

    define( 'CUSTOM_FILTER_POST_TYPE',  'feature');
    define( 'CUSTOM_FILTER_TAXONOMY',   'feature_category');

    add_action('restrict_manage_posts', 'custom_post_type_filter');

    function custom_post_type_filter() 
        {
            global $typenow;
            
            // Check if we are on the 'feature' custom post type
            if ($typenow ==  CUSTOM_FILTER_POST_TYPE ) {
                // Get the taxonomy terms for your custom taxonomy 'feature_category'
                $terms = get_terms( CUSTOM_FILTER_TAXONOMY );
                
                // Check if there are any terms
                if ($terms) {
                    echo '<select name="' . CUSTOM_FILTER_TAXONOMY . '_filter" id="'. CUSTOM_FILTER_TAXONOMY .'_filter">';
                    echo '<option value="">Select Category</option>';
                    
                    // Loop through each term and display as options in the dropdown
                    foreach ($terms as $term) {
                        echo '<option value="' . $term->slug . '"' . selected($_GET[ CUSTOM_FILTER_TAXONOMY. '_filter'], $term->slug, false) . '>' . $term->name . '</option>';
                    }
                    
                    echo '</select>';
                }
            }
        }

    // Hook to the 'parse_query' action hook to filter the query based on the selected category
    add_action('parse_query', 'filter_feature_posts');

    function filter_feature_posts($query) 
        {
            global $pagenow;
            $post_type = CUSTOM_FILTER_POST_TYPE;
            
            if (is_admin() && $pagenow == 'edit.php' && isset($_GET[ CUSTOM_FILTER_TAXONOMY . '_filter']) && $_GET[ CUSTOM_FILTER_TAXONOMY . '_filter'] != '') {
                $query->query_vars['tax_query'] = array(
                    array(
                        'taxonomy'  => CUSTOM_FILTER_TAXONOMY,
                        'field'     => 'slug',
                        'terms'     => $_GET[ CUSTOM_FILTER_TAXONOMY . '_filter']
                    )
                );
            }
        }

Adding custom filters to your WordPress dashboard enhances the usability of your content management system. In this article, we’ve explored the process of adding a drop-down filter for a custom post type. By leveraging WordPress hooks and functions, you can empower content managers to efficiently organize and locate specific content within custom post types.

Remember to customize the code to fit the specifics of your project. As you implement these enhancements, you’ll contribute to a more user-friendly and streamlined WordPress experience for both content creators and administrators.