Home › Forums › Backend Issues (wp-admin) › Add custom column and make it sortable
Hi
I am having a problem with making a custom column sortable.
This is OK:
– Added i new field to existing CPT
– Added custom column to CPT posts page
– Made colun sortable
This is the problem:
When i sort by the ACF field the result displayed is only the posts that have been saved/updated after i have added the field.
How can i fix this?
I am running version 3.5.8.1 (several critical installs and upgrade is on the list…)
This is my code
// Make column E-mail reminders sortable
function sba_sortable_columns( $columns ) {
$columns['email_reminder'] = 'email_reminder';
//To make a column 'un-sortable' remove it from the array
//unset($columns['date']);
return $columns;
}
add_filter( 'manage_edit-sba_control_sortable_columns', 'sba_sortable_columns' );
//Tell WP how to sort 'email_reminder'
// This dosent work as expected because the meta field dosent exist
//before a post is updated so when sorting the column 'email_reminder' only the
//updated + new posts is dispalyed!
//Send support ticket to ACF alternative is to bulk update all controls manually.
function email_reminder_orderby( $query ) {
if( ! is_admin() )
return;
$orderby = $query->get( 'orderby');
if( 'email_reminder' == $orderby ) {
$query->set('meta_key','control_reminders_enabled');
$query->set('orderby','meta_value_num');
}
}
add_action( 'pre_get_posts', 'email_reminder_orderby' );

Hi @torbjorn_s
Your query is filtering the posts to those that have a value for the control_reminders_enabled meta_key.
If your posts does not yet have a value for this, then your query will not work.
Perhaps you need to also add in the ability to treat posts without a value as a value of 0
I’m not 100% sure how to do this, but chances are someone on stackexcahnge / google has already written / shared the code.
Good luck
Cheers
E
Does anybody in this forum have any tips on how i can treat posts without a value as 0?
The topic ‘Add custom column and make it sortable’ is closed to new replies.
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.