Home › Forums › General Issues › Add Sortable ACF Admin Column › Reply To: Add Sortable ACF Admin Column
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
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.