I'm trying to get my admin columns on edit-tags.php to be sortable by ACF field value, but I'm missing something. I can click the column heading to sort the column, but it only sorts the table by tag name and not the ACF field value in the column. If anyone can help, I'd really appreciate it!
I have set up the columns that I want to add:
/**
* Add columns to the post_tags admin table
*/
function add_post_tag_columns($columns){
$columns['featured'] = 'Featured';
$columns['order'] = 'Order';
$columns['group'] = 'Group';
// Remove description column from table
unset( $columns['description'] );
return $columns;
}
add_filter('manage_edit-post_tag_columns', 'add_post_tag_columns');
I have set the value for the tags in each column:
/**
* Set values for new post_tags admin table columns
*/
function add_post_tag_column_content( $content, $column_name, $term_id ){
$term= get_term($term_id, 'post_tag');
switch ( $column_name ) {
// display the value of ACF fields
case 'featured' :
$content = the_field( 'kw_featured_tag', $term );
break;
case 'order' :
$content = the_field( 'kw_tag_order', $term );
break;
case 'group' :
$content = the_field( 'kw_group_tags', $term );
break;
}
return $content;
}
add_filter('manage_post_tag_custom_column', 'add_post_tag_column_content', 10, 3);
I have set up column sorting
/**
* Make columns on post_tags admin table sortable
*/
function set_custom_post_tag_sortable_columns( $sortable_columns ) {
$sortable_columns['featured'] = 'kw_featured_tag';
$sortable_columns['order'] = 'kw_tag_order';
$sortable_columns['group'] = 'kw_group_tags';
return $sortable_columns;
}
add_filter( 'manage_edit-post_tag_sortable_columns', 'set_custom_post_tag_sortable_columns' );
And here's where [I think] I'm having the issue:
/**
* Set up the orderby for the column sorting
*/
function post_tag_custom_orderby( $query ) {
if ( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if ( 'featured' == $orderby ) {
$query->set( 'meta_key', 'kw_feature_tag' );
$query->set( 'orderby', 'meta_value_num' );
}
elseif ( 'order' == $orderby ) {
$query->set( 'meta_key', 'kw_tag_order' );
$query->set( 'orderby', 'meta_value_num' );
}
elseif ( 'group' == $orderby ) {
$query->set( 'meta_key', 'kw_group_tags' );
$query->set( 'orderby', 'meta_value' );
}
}
add_action( 'pre_get_posts', 'post_tag_custom_orderby' );
If anyone could help narrow down what I've done wrong, I would be a very happy Wordpress developer :P. Thanks in advance!
--
Edit: Here's a couple screenshots of my admin table:
- Featured (kw_featured_tag) is a true/false
- Order (kw_tag_order) is a number field
- Group (kw_group_tags) is a checkbox array (I changed it to return an array and now I need to fix my code to get the list of items in the array to be displayed instead of just "array")