Custom Table Column Sortable by Taxonomy Query
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.
Topic screen-columns wp-list-table pre-get-posts dashboard sort Wordpress
Category Web