Most information on your pages and posts in the WordPress Admin is organized into tables. Anyone who uses WordPress will be familiar with these. They work well and allow you to undertake ‘Quick Edits’ of certain page or post information, show you the date the post or page was published and a lot more, besides. In addition you might have noticed that you can filter these tables with a selection of dropdown menus that appear at the top of each table.
There are only a handful of filters however. So, what do you do if you want to filter a value that doesn’t have a filter box in the Admin? Fortunately WordPress does allow us to add our own filters. We’re going to see how.
Why Use Custom Filters?
So, why exactly would you need any custom filters for organizing the Admin tables in WordPress? Let’s imagine we have a site that has a large number of post Authors and a lot of posts.
You can view the Posts author from the Admin table without any modification. What you can’t currently do is sort Posts by Author. Clearly this is a significant limitation if you wanted to be able to easily view all Posts written by a particular author. What would help is if you could view a dropdown menu of all the Post authors and then select one from the list. That could really speed things up.
Let’s take a look at how we could build this.
Creating Our Custom Filter
To help you build your own filters, WordPress provides the restrict_manage_posts
and pre_get_posts
hooks.
The restrict_manage_posts
is introduced in the wp-admin/includes/class-wp-posts-list-table.php
file around line 557 and fires before the Filter button is created above the admin table list.
We will use a custom function and link it with the restrict_manage_posts
hook as usual (take a look here for more information on this) . Go to your active theme’s main folder and open the functions.php
file with your favorite editor. At the end of the file’s contents insert the code below:
function my_author_filter(){
$screen = get_current_screen();
global $post_type;
if( $screen->id == 'edit-post' ){
$my_args = array(
'show_option_all' => 'All Authors',
'orderby' => 'display_name',
'order' => 'ASC',
'name' => 'authors_admin_filter',
'who' => 'authors',
'include_selected' => true
);
if(isset($_GET['authors_admin_filter'])){
$my_args['selected'] = (int)sanitize_text_field($_GET['authors_admin_filter']);
}
wp_dropdown_users($my_args);
}
}
add_action('restrict_manage_posts','my_author_filter');
The main condition we used is if( $screen->id == 'edit-post')
. Here we used the get_current_screen()
WordPress function which returns the screen id we are on.
In this instance the admin screen ID is ‘edit-post’. In other words, we make sure we are on the posts administration screen. If for example, we wanted to apply this to the pages admin screen we should use if( $screen->id == 'edit-page'
or if we were working for a custom post type, the edit-{post_type}
would be the correct if statement.
Note: To see which ID corresponds to each admin screen you can check the official documentation about the admin screen reference.
In the code below we check if an author is already selected and if so, we apply the selected value:
if(isset($_GET['authors_admin_filter'])){
$my_args['selected'] = (int)sanitize_text_field($_GET['authors_admin_filter']);
}
Finally, by making use of the wp_dropdown_users
WordPress function we will generate the drop down list of the authors according to the arguments we defined previously.
Next we chose the text that will display the ‘show-all’ option in the show_option_all
argument. We chose the “All authors” string. In the ‘name’ argument we fill in the desired name for the drop down list. We will use this value later to filter the posts list results.
In the ‘who’ argument we define which type of users to query, in our case ‘authors’. With the ‘include_selected’ argument we choose whether we want to include the selected user ID in the drop-down list.
At this point, if you refresh the admin posts screen, you should see the author drop down menu next to the other built-in filter menus.
If you select a value and press “Filter” the value will stay selected upon the page reload, but the results will remain unchanged. In our next step, we will make the necessary changes in order to filter the post list to show only the posts linked to the the author we selected.
Filtering the Results
Once again, the pre_get_posts
filter will do the work. We have examined this hook in our article WordPress Admin Search: Extending the Results. We’ll use this again to alter the query variable object that is created in the post admin screen, before the actual query is run. In your functions.php
insert the following lines:
function my_author_filter_results($query){
$screen = get_current_screen();
global $post_type;
if ( $screen->id == 'edit-post' ) {
if(isset($_GET['authors_admin_filter'])){
$author_id = sanitize_text_field($_GET['authors_admin_filter']);
if($author_id != 0){
$query->query_vars['author'] = $author_id;
}
}
}
}
add_action('pre_get_posts','my_author_filter_results');
The main condition, as previously, is necessary here as well to ensure we are on the admin posts screen.
The $author_id = sanitize_text_field($_GET['authors_admin_filter']);
keeps the selected author id in the $author_id
variable. After this, and if the author id is not 0(All Authors are selected), we pass the $author_id
value in the query’s author parameter.
Try our Award-Winning WordPress Hosting today!
And that’s it. Now the posts shown will be only the ones that belong to the selected author/user id.
Conclusion
Custom filtering in the Admin screens can make your content easily searchable by Admin users, something which can be invaluable when you have a lot of posts or pages. And of course, this is just one possible way of using a filter in the Admin tables. There are a ton of alternate ways that you might find it useful and hopefully the techniques we’ve outlined here will help you create your own filters.
For example you might want to create a drop down filter menu with the options to show posts created over the last 30 days or perhaps a filter that sorts posts by any taxonomy. The query_vars list can help you brainstorm possible options!.
Have fun playing around with this great feature!
See Also
WordPress Admin List Tables: Add Custom Filters
Getting Started with WordPress Custom Fields
Start Your 14 Day Free Trial
Try our award winning WordPress Hosting!