Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.0 kB
6
Indexable
// Add the custom column to the users list table
add_filter('manage_users_columns', function($columns) {
    $columns['salesking_agentid'] = 'Agent ID';
    return $columns;
});

// Display the value for the custom column
add_filter('manage_users_custom_column', function($output, $column_name, $user_id) {
    if ($column_name === 'salesking_agentid') {
        $agent_id = get_user_meta($user_id, 'salesking_agentid', true);
        return $agent_id ? esc_html($agent_id) : '—';
    }
    return $output;
}, 10, 3);

// Make the column sortable
add_filter('manage_users_sortable_columns', function($columns) {
    $columns['salesking_agentid'] = 'salesking_agentid';
    return $columns;
});

// Handle the sorting
add_action('pre_get_users', function($query) {
    if (!is_admin()) {
        return;
    }

    $orderby = $query->get('orderby');
    
    if ('salesking_agentid' === $orderby) {
        $query->set('meta_key', 'salesking_agentid');
        $query->set('orderby', 'meta_value');
    }
});
Leave a Comment