I am looking to add a custom column to a custom post type post listing table in the dashboard.
I have read many questions / answers on WPSE, along with this article. Although, it seems like everyone wants to query by meta key / value.
I am trying to add a taxonomy value (basically a category) to a custom post type table.
I would like the column to be sortable, however I am not understanding the query adjustment.
Add the column title.
function mbe_column_titles($columns){
$columns['title'] = 'Frequently Asked Question';
$columns['mbe-faq-category'] = 'Category';
return $columns;
}
add_filter('manage_mbe-faqs_posts_columns', 'mbe_column_titles');
Add the row values.
function mbe_column_rows($column_name, $post_id){
if($column_name == 'mbe-faq-category'){
$categories = wp_get_object_terms($post_id, 'mbe-faq-categories');
$the_category = array();
if($categories){
foreach($categories as $category){
$the_category[] = $category->name;
}
}
echo join(', ', $the_category);
}
}
add_action('manage_mbe-faqs_posts_custom_column', 'mbe_column_rows', 10, 2);
Prepare the ordering key,
function mbe_sortable_columns($columns){
$columns['mbe-faq-category'] = 'mbe-faq-category';
return $columns;
}
add_filter('manage_edit-mbe-faqs_sortable_columns', 'mbe_sortable_columns');
I am stumped on how to actually sort the column by the FAQ Category. I would assume this would be a taxonomy query. I am too embarrassed to even post my attempt of handling this, and everything I do seems to fail. I would like to use the pre_get_posts
filter to handle this if possible.
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:YOUR-POST-TYPE-NAME
with your actual post type name.YOUR-TAXONOMY-NAME
with your actual taxonomy name.YOUR COLUMN NAME
with your actual column name.YOUR-COLUMN-SLUG
with your actual column slug.Step 1
Add Additional WordPress Admin Table Columns
Step 2
Add All Assigned Linkable Taxonomy Terms as Row Data Within Custom WordPress Admin Table Column
Step 3
Enable Custom WordPress Admin Table Column to Become Sortable
Step 4
Modify
post_clauses
to Allow Sorting Custom WordPress Admin Table Columns by a Taxonomy TermStep 5 (BONUS)
Adjust the Width of Custom WordPress Admin Table Columns
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.This can be done a lot easier with a single filter:
Replace
{post_type}
and{taxonomy}
with the post type and taxonomy you’re working with. The end result will be a custom column on your custom post type that is sortable, and can be used to sort the posts in theWP_List_Table
.