1

This question seems simple enough, and I'm surprised that I don't find much while searching Google to find an answer. The only thing I've found is a pro plugin, so far.

I'd just like to be able to sort by multiple columns in the admin area. So, for example, in the Pages section, there are three columns: Title, Author, and Date. What would I need to do to sort by Author, and then for the first author, alphabetize all of their Pages by title (this is assuming that author has already been made sortable on its own, using something like the manage_edit-page_sortable_columns filter)?

I'd prefer to do this with code in the functions.php file, instead of a plugin, but anything would be great.

1

Since WordPress already allows for the sorting of page titles alphabetically (either ascending or descending order), all that is actually needed is to add an Author filter.

This can be achieved by adding the following to the functions.php file of your active theme:

function rudr_filter_by_the_author() {
    $params = array(
        'name' => 'author', // this is the "name" attribute for filter <select>
        'show_option_all' => 'All authors' // label for all authors (display posts without filter)
    );

    if ( isset($_GET['user']) )
        $params['selected'] = $_GET['user']; // choose selected user by $_GET variable

    wp_dropdown_users( $params ); // print the ready author list
}

add_action('restrict_manage_posts', 'rudr_filter_by_the_author');

I found the above snippet at Misha Rudrastyh's website and have tested it on my own WordPress admin area without a problem.

All it does is add an Author filter to the admin area posts and pages sections. You can then filter your posts/pages by author, and then sort them by title (or date) in ascending/descending order as preferred.

But keep in mind that having this in functions.php of your theme means this feature will only be available as long as that theme remains active. If that is a problem for you, just make this snippet into a plugin of your own to activate/deactivate when needed.

So to turn this into a plugin that will work with any theme, save the following into a file called admin-area-author-filter.php and then upload it to your WordPress plugin directory:

<?php

/*
Plugin Name: Admin Area Author Filter
Description: Adds an author filter to the posts and pages sections of the admin area. Snippet taken from: https://rudrastyh.com/wordpress/filter-posts-by-author.html
*/

function rudr_filter_by_the_author() {
    $params = array(
        'name' => 'author', // this is the "name" attribute for filter <select>
        'show_option_all' => 'All authors' // label for all authors (display posts without filter)
    );

    if ( isset($_GET['user']) )
        $params['selected'] = $_GET['user']; // choose selected user by $_GET variable

    wp_dropdown_users( $params ); // print the ready author list
}

add_action('restrict_manage_posts', 'rudr_filter_by_the_author');

?>

Then just activate it and you are good to go.

Thank you for asking this question, by the way. It made me curious and now I have a handy author filter in my admin area, too. I'm surprised this isn't a core feature like the category filter in the posts area. It's very useful, so thank you again for bringing this issue to my attention.

  • This is a great answer. I'm afraid my question was too simple. I'm working with a CPT and just used Pages as an example. But your answer gives me an idea to see if I can filter my custom meta column, and then sort by title. I'm going to research this. Thanks! – Mark Mar 14 at 13:18
  • This should also work with custom post types - but I don't have any active on my current installation so I can't test it and confirm. Please try it and see if the Authors filter shows up on your custom post types as well and let us know of the result. – jsmod Mar 14 at 14:38
  • I'm sorry, I'm trying to actually filter by a custom field, not author. I just used author as a simple example at first to make the question as basic as possible. I'm researching now if I can filter by a custom meta field. – Mark Mar 14 at 14:41
  • I've opened a new question here, based on what I learned from this question: wordpress.stackexchange.com/questions/331647/… – Mark Mar 14 at 14:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.