Custom Table Column Sortable by Taxonomy Query

To achieve adding a custom sortable column to the WP_List_Table of your post type within the WordPress administration back-end dashboard, you will need to do the following:

  1. Replace all occurrences of YOUR-POST-TYPE-NAME with your actual post type name.
  2. Replace all occurrences of YOUR-TAXONOMY-NAME with your actual taxonomy name.
  3. Replace all occurrences of YOUR COLUMN NAME with your actual column name.
  4. Replace all occurrences of YOUR-COLUMN-SLUG with your actual column slug.

Step 1

Add Additional WordPress Admin Table Columns

if(!function_exists('mbe_change_table_column_titles')){
    function mbe_change_table_column_titles($columns){
        unset($columns['date']);// temporarily remove, to have custom column before date column
        $columns['YOUR-COLUMN-SLUG'] = 'YOUR COLUMN NAME';
        $columns['date'] = 'Date';// readd the date column
        return $columns;
    }
    add_filter('manage_YOUR-POST-TYPE-NAME_posts_columns', 'mbe_change_table_column_titles');
}

Step 2

Add All Assigned Linkable Taxonomy Terms as Row Data Within Custom WordPress Admin Table Column

if(!function_exists('mbe_change_column_rows')){
    function mbe_change_column_rows($column_name, $post_id){
        if($column_name == 'YOUR-COLUMN-SLUG'){
            echo get_the_term_list($post_id, 'YOUR-TAXONOMY-NAME', '', ', ', '').PHP_EOL;
        }
    }
    add_action('manage_YOUR-POST-TYPE-NAME_posts_custom_column', 'mbe_change_column_rows', 10, 2);
}

Step 3

Enable Custom WordPress Admin Table Column to Become Sortable

if(!function_exists('mbe_change_sortable_columns')){
    function mbe_change_sortable_columns($columns){
        $columns['YOUR-COLUMN-SLUG'] = 'YOUR-COLUMN-SLUG';
        return $columns;
    }
    add_filter('manage_edit-YOUR-POST-TYPE-NAME_sortable_columns', 'mbe_change_sortable_columns');
}

Step 4

Modify post_clauses to Allow Sorting Custom WordPress Admin Table Columns by a Taxonomy Term

if(!function_exists('mbe_sort_custom_column')){
    function mbe_sort_custom_column($clauses, $wp_query){
        global $wpdb;
        if(isset($wp_query->query['orderby']) && $wp_query->query['orderby'] == 'YOUR-COLUMN-SLUG'){
            $clauses['join'] .= <<<SQL
LEFT OUTER JOIN {$wpdb->term_relationships} ON {$wpdb->posts}.ID={$wpdb->term_relationships}.object_id
LEFT OUTER JOIN {$wpdb->term_taxonomy} USING (term_taxonomy_id)
LEFT OUTER JOIN {$wpdb->terms} USING (term_id)
SQL;
            $clauses['where'] .= "AND (taxonomy = 'YOUR-TAXONOMY-NAME' OR taxonomy IS NULL)";
            $clauses['groupby'] = "object_id";
            $clauses['orderby'] = "GROUP_CONCAT({$wpdb->terms}.name ORDER BY name ASC)";
            if(strtoupper($wp_query->get('order')) == 'ASC'){
                $clauses['orderby'] .= 'ASC';
            } else{
                $clauses['orderby'] .= 'DESC';
            }
        }
        return $clauses;
    }
    add_filter('posts_clauses', 'mbe_sort_custom_column', 10, 2);
}

Step 5 (BONUS)

Adjust the Width of Custom WordPress Admin Table Columns

if(!function_exists('mbe_print_admin_styles')){
    function mbe_print_admin_styles(){
        if(!is_admin()){
            return false;
        }
        global $pagenow;
        if($pagenow == 'edit.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'YOUR-POST-TYPE-NAME'){
            echo '
        <style type="text/css">
            .column-YOUR-COLUMN-SLUG{
                width: 10%;
            }
        </style>
        '.PHP_EOL;
        }
    }
    add_action('wp_print_scripts', 'mbe_print_admin_styles');
}

Thanks to @goto10 for asking Sortable admin columns, when data isn’t coming from post_meta and @scribu for posting Custom Sortable Columns and Sortable Taxonomy Columns for this answer to the original question Custom Table Column Sortable by Taxonomy Query.

Leave a Comment