Home › Forums › General Issues › Add Sortable ACF Admin Column
I’m looking to add a very simple sort-able custom admin column from the “select” type. I simply want the “Primary Category” field to show in the admin column, and I want to be able to sort it alphabetically between the options (Dining, Lodging, Bars). Info:
Custom post type – “venues”
ACF field – “priamry_category” (“select” type, willing to change if needed)
I’ve been reading a lot about doing this for awhile now, but I can’t quite find what I’m looking for. I saw there was a plugin for making the fields sort-able, and I’d prefer to avoid using that, as the code addition should be just a few lines. If someone could walk me through this, I’d be grateful.
Here’s my current code, which shows the fields and proper value, I just need sorting now.
EDIT
I seem to have figured it out. I’m not sure why “edit-venues” has to go in the last piece of code, but it wouldn’t work unless I did, despite a tutorial showing “edit-type” being in the first filter as well. Any ideas? (seems to be working, regardless).
add_filter('manage_venues_posts_columns', 'filter_venues_custom_columns');
function filter_venues_custom_columns($columns) {
$columns['primary_category'] = 'Primary Category';
return $columns;
}
add_action('manage_venues_posts_custom_column', 'action_venues_custom_columns');
function action_venues_custom_columns($column) {
global $post;
if($column == 'primary_category') {
echo(get_field('primary_category', $post->ID));
}
}
add_filter( 'manage_edit-venues_sortable_columns', 'sortable_venues_custom_columns' );
function sortable_venues_custom_columns( $columns ) {
$columns['primary_category'] = 'title';
//To make a column 'un-sortable' remove it from the array
//unset($columns['date']);
return $columns;
}
It looks like you have everything you need except a `pre_get_posts’ filter.
I recently did this on a site I was working on. Basically, you need to add a filter.
Here is the filter that I used:
function reference_pre_get_posts($query) {
// only in admin
if (!is_admin() || !$query->is_main_query()) {
return;
}
if ($query->query_vars['post_type'] == 'reference' &&
($orderby = $query->get('orderby'))) {
switch ($orderby) {
case 'featured':
case 'reference_featured':
$query->set('meta_key', 'reference_featured');
$query->set('orderby', 'meta_value_num');
break;
default:
// do nothing
break;
}
} // end if reference and orderby
}
basically it test to see if we’re in the admin and if we are querying my post type and it it is one of the meta_keys I’ve set up as a sortable field. If it is then I add the meta_key that it needs to be ordered by and change the orderby to meta_value.
Hope this helps
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.