How to add a filter to Post Listing in WordPress

  • admin
  • Tag 1
    Tag 2
    Tag 3
    Tag 4

    With another new project, comes another chance to learn something new. With this project, the client wanted the ability to filter Woocommerce products by Stock Status. Out of the box, it doesn’t come with this ability, so as is my job, I set about adding this functionality. This task ended up being quite easy, and after a bit of researching to locate the proper filter, it was time to write some code.

    We start off with out filters. We want to tap into the action that WordPress uses to build it’s post column, so we start by adding our hook for this:

    add_action( 'restrict_manage_posts', 'add_filter_to_menu');

    With our hook in place, we now add the function. Don’t forget to sanitize user input for security.

    
       function add_filter_to_menu (){
    
            if ($_GET['post_type'] == 'product'){
    
                $values = array(
                    'In Stock' => 'instock', 
                    'Out of Stock' => 'outofstock',
                );
                ?>
                <select name="product_filter_by_stock_status">
                <option value="">Filter By Stock Status</option>
                <?php
                    
                    // validate data to make sure it's what we expect. 
    
                    $current_v = (in_array($_GET['product_filter_by_stock_status'], array('instock', 'outofstock'))) ? $_GET['product_filter_by_stock_status'] : ""; 
                    
                    foreach ($values as $label => $value) {
                        printf
                            (
                                '<option value="%s"%s>%s</option>',
                                $value,
                                $value == $current_v? ' selected="selected"':'',
                                $label
                            );
                        }
                ?>
                </select>
                <?php
            }
    
        }
    
    

    Now with that in place, we’ve added our select menu, which will look something like this:

    menu_bar

    With that done, we move on to the actual post filter. First we add our action hook:

    add_filter( 'parse_query', 'filter_the_posts' );

    And then the function to handle the filtering. Again, it’s important to remember to validate data before using it, especially when working with the database queries.

    
       function filter_the_posts( $query ){
        
            global $pagenow;
        
            if ( $_GET['post_type'] == 'product' 
                && is_admin() && $pagenow=='edit.php' 
                && isset($_GET['product_filter_by_stock_status']) 
                && $_GET['product_filter_by_stock_status'] != ''
                && in_array($_GET['product_filter_by_stock_status'], array('instock', 'outofstock'))
                ) {
                    
                $query->query_vars['meta_key'] = '_stock_status';
                $query->query_vars['meta_value'] = $_GET['product_filter_by_stock_status'];
            }
            
        }
    
    

    And there you have it. With 2 simple functions, we make another client extremely happy.

    Leave a comment

  • (required)