moonorongo
12/22/2016 - 3:56 PM

[Wordpress Filters ADMIN] add filter to post list in Administrator #wordpress #php

[Wordpress Filters ADMIN] add filter to post list in Administrator #wordpress #php

<?php
    function add_brands_filter_to_post_administration(){
        global $post_type;
        if($post_type == 'campaign'){

            $brand_args = array(
                'show_option_all'   => 'All Campaigns',
                'orderby'           => 'NAME',
                'order'             => 'ASC',
                'name'              => 'brand_admin_filter',
                'taxonomy'          => 'brands'
            );

            //if we have a post format already selected, ensure that its value is set to be selected
            if(isset($_GET['brand_admin_filter'])){
                $brand_args['selected'] = sanitize_text_field($_GET['brand_admin_filter']);
            }

            wp_dropdown_categories($brand_args);
        }
    }
    add_action('restrict_manage_posts','add_brands_filter_to_post_administration');
    

    
    function add_brands_filter_to_posts($query){
        global $post_type, $pagenow;

        //if we are currently on the edit screen of the post type listings
        if($pagenow == 'edit.php' && $post_type == 'campaign'){
            if(isset($_GET['brand_admin_filter'])){
                $brand = sanitize_text_field($_GET['brand_admin_filter']);
                if($brand != 0){

                    $query->query_vars['tax_query'] = array(
                        array(
                            'taxonomy'  => 'brands',
                            'field'     => 'ID',
                            'terms'     => array($brand)
                        )
                    );

                }
            }
        }   

    }
    add_action('pre_get_posts','add_brands_filter_to_posts');