sort - Admin multiple column sorting

admin2025-06-02  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.

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.

Share Improve this question asked Mar 13, 2019 at 21:48 MarkMark 3211 gold badge3 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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/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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748808872a313897.html

最新回复(0)