Customised WordPress Admin Filters in the backoffice

Creating the Select Drop Down Using the ‘restrict_manage_posts’ Filter

The first thing that needs to be done is the creation of the select drop down for the two additional filters we will be making.

You will need to navigate over to your child themes functions.php file (or another applicable file) and add code which will add these two new filters to your posts administration screen.

Filtering by Author

//defining the filter that will be used to select posts by 'post formats'
function add_post_formats_filter_to_post_administration(){
    //execute only on the 'post' content type
    global $post_type;
    if($post_type == 'post'){

        $post_formats_args = array(
            'show_option_all'   => 'All Post formats',
            'orderby'           => 'NAME',
            'order'             => 'ASC',
            'name'              => 'post_format_admin_filter',
            'taxonomy'          => 'post_format'
        );

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

        wp_dropdown_categories($post_formats_args);

    }
}
add_action('restrict_manage_posts','add_post_formats_filter_to_post_administration');

Filtering the Posts Using the ‘pre_get_posts’ Filter

Now that we have an interface in which the user can select their post format and / or authors, we need to filter the listing of posts based on these drop downs.

We will do this by hooking into the pre_get_posts filter.

The pre_get_postsfilter has access to the current query before it is executed by WordPress and will let us change the posts that will be fetched from the database.

The overall idea is to modify the query in such as way as to restrict which posts will be brought into the administration listing. To do this for both post formats and authors is slightly different so I have broken down each method below

Filtering by Author

//restrict the posts by an additional author filter
function add_author_filter_to_posts_query($query){

    global $post_type, $pagenow; 

    //if we are currently on the edit screen of the post type listings
    if($pagenow == 'edit.php' && $post_type == 'post'){

        if(isset($_GET['aurthor_admin_filter'])){

            //set the query variable for 'author' to the desired value
            $author_id = sanitize_text_field($_GET['aurthor_admin_filter']);

            //if the author is not 0 (meaning all)
            if($author_id != 0){
                $query->query_vars['author'] = $author_id;
            }

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

Views (24)

Share on Facebook0Share on Google+0Share on LinkedIn0Tweet about this on TwitterEmail this to someonePrint this page
[Total: 1    Average: 5/5]

Leave a Reply

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

seven − five =