0

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.

Here is my updated code, still doesn't work:

// 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_organization'] = __( 'Organization', 'ultimate-member' );
  $columns['user_role'] = __( 'Organization Role', 'ultimate-member' );
    $columns['user_company'] = 'User Company';
    $columns['registration_date'] = 'Registration Date';
  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_organization' ) {
    um_fetch_user( $user_id );
    $value = um_user( 'user_organization' );
    if( empty( $value )) {
        $value = '-';
    }
    }
    if ( $column_name == 'user_role' ) {
    um_fetch_user( $user_id );
    $value = um_user( 'user_role' );
    if( empty( $value )) {
        $value = '-';
    }
    }
    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 '-';
        }
    }
    if ( $column_name == 'registration_date' ) {
        $date_format = 'M j, Y';
    $value = date( $date_format, strtotime( get_the_author_meta( 'registered', $user_id ) ) );
    if( empty( $value )) {
        $value = '-';
    }
    }
    return $value;
}

// 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_organization'] = 'user_organization';
  $columns['user_role'] = 'user_role';
    $columns['user_company'] = 'user_company';
    $columns['account_status'] = 'account_status';
    $columns['registration_date'] = 'registered';
  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_organization' ) {
        $query->set( 'orderby',  'meta_value' );
        $query->set( 'meta_key', 'user_organization' );
  }
  if ( $query->get( 'orderby' ) == 'user_role' ) {
        $query->set( 'orderby',  'meta_value' );
        $query->set( 'meta_key', 'user_role' );
  }
    if ( $query->get( 'orderby' ) == 'account_status' ) {
        $query->set( 'orderby',  'meta_value' );
        $query->set( 'meta_key', 'account_status' );
  }
    if ( $query->get( 'orderby' ) == 'registration_date' ) {
        $query->set( 'orderby',  'meta_value' );
        $query->set( 'meta_key', 'registered' );
  }
}

// Make ACF column on User's page sortable
if ( is_admin() ) {
    add_action('pre_user_query', 'sort_users_by_acf_company_field');
}
function sort_users_by_acf_company_field( $userquery ){
  if ( 'user_company'==$userquery->query_vars['orderby'] ) {
    global $wpdb;
    $userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";//note use of alias
    $userquery->query_where .= " AND alias.meta_key = 'user_company' ";//which meta are we sorting with?
    $userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
  }
}

1 Answer 1

0

You can use the "pre_user_query" filter hook to modify the query that retrieves the users.

Here's an example code that how to sort the Users page admin column with an ACF field named.

function sort_users_by_acf_field( $query ) {
    if ( ! is_admin() ) {
        return;
    }
    if ( isset( $query->query_vars['orderby'] ) && 'user_company_name' === $query->query_vars['orderby'] ) {
        $query->query_vars['meta_key'] = 'user_company_name';
        $query->query_vars['orderby'] = 'meta_value';
    }
}
add_action('pre_user_query','sort_users_by_acf_field');

Hope this will help!

3
  • Thank you for your reply. I'm still very new to hooks so not sure if I'm doing it correctly. I replaced the last action/function I have in the code above with yours and that didn't seem to do anything. It still sorts based on username.
    – Amy Ling
    Commented Feb 16, 2023 at 21:21
  • Did you put your meta_key in my code? Can you provide more details Commented Feb 17, 2023 at 14:06
  • I did change the meta_key to user_company. Is it because the acf field is a post object and only results in a postID and that's why it can't sort by the post title? I posted the code i'm currently using in my original post above.
    – Amy Ling
    Commented Mar 9, 2023 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.