Home › Forums › General Issues › Sorting Users page admin column with ACF field
I have a CPT called Companies. I created an ACF relationship field called user_company that gets displayed in the Users profile page.
When a user registers, I have to go in and assign them to a company. I added the ACF field to the Users page admin column but I am unable to sort it. It currently sorts based on post_id and I have no idea how to sort it by post_title.
Here is the code I have so far:
// Add columns and data to Users admin page
add_filter( 'manage_users_columns', 'manage_users_columns_new_columns' );
function manage_users_columns_new_columns( $columns ) {
$columns['user_comp any'] = 'User Company';
return $columns;
}
add_filter( 'manage_users_custom_column', 'manage_users_custom_column_new_columns', 10, 3 );
function manage_users_custom_column_new_columns( $value, $column_name, $user_id ) {
if ( $column_name == 'user_company' ) {
$user_company = get_field('user_company', 'user_' . $user_id);
$user_company_post = get_post($user_company);
if( isset( $user_company_post ) ){
return $user_company_post->post_title;
} else {
return '-';
}
}
return $value;
}
// ACF/save_post to save title of User Company as another meta_value
function save_user_company_name ( $post_id ) {
$user_company_id = get_field( 'user_company', $post_id );
if ( $user_company_id ) {
update_post_meta( $post_id, 'user_company_name', get_post_field( 'post_title', $user_company_id ) );
}
}
add_action( 'acf/save_post', 'save_user_company_name', 20 );
// Sort columns on Users admin page
add_filter( 'manage_users_sortable_columns', 'register_sortable_columns_custom', 10, 1 );
function register_sortable_columns_custom( $columns ) {
$columns['user_company'] = 'user_company';
return $columns;
}
add_action( 'pre_get_users', 'pre_get_users_sort_columns_custom' );
function pre_get_users_sort_columns_custom( $query ) {
if ( $query->get( 'orderby' ) == 'user_company' ) { // Currently sorts by post ID vs post title. Will need to add additional code: https://support.advancedcustomfields.com/forums/topic/admin-column-a-z-sorting-for-object-field/
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'user_company_name' );
}
}
I added the acf/save_post portion based on answers I have found online but I don’t know how that works. I tried saving each Company posts as well as the Users assigned to a company. But when I try to sort on the admin page, everything blanks out. I’m not sure how to get this to work.
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 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.