Get user metadata and display in WP_List_Table

无人久伴 提交于 2019-12-25 01:28:37

问题


I'm trying to get user metada in this case I will find for username or user firstname or lastname from within WP_List_Table class. Since I have a field called users_id where I store the wp_users.ID value then I did this in function get_sql_results():

private function get_sql_results()
{
    global $wpdb;

    $search = ( isset($_REQUEST['s']) ) ? $_REQUEST['s'] : false;
    $do_search = ( $search ) ? " WHERE firstname LIKE '%$search%' OR lastname LIKE '%$search%' OR foid LIKE '%$search%' OR no_frequent LIKE '%$search%' " : '';

    $args = array('id', 'firstname', 'lastname', 'foid', 'no_frequent', 'users_id', 'create_time');
    $sql_select = implode(', ', $args);
    $sql_results = $wpdb->get_results("SELECT " . $sql_select . " FROM " . $wpdb->prefix . "ftraveler $do_search ORDER BY $this->orderby $this->order ");

    return $sql_results;
}

So I get there the users_id. Then in the function get_columns() I show the value as follow:

public function get_columns()
{
    $columns = array(
        'id' => __('ID'),
        'users_id' => __('WP User'),
        'firstname' => __('Firstname'),
        'lastname' => __('Lastname'),
        'foid' => __('Passport'),
        'no_frequent' => __('Frequent No'),
        'create_time' => __('Created on')
    );
    return $columns;
}

But that show the user ID when I should show the username or firstname or lastname and there is where my doubt comes, how I can get, using get_user_metadata($user_id) function, that values and display in the result table?


回答1:


You have to iterate into the $sql_results and replace users_id with your custom get_user_metadata($users_id). Something like:

foreach( $sql_results as $key => $value ) {
    $new_value = do_something_with( get_user_metadata( $value->users_id ) );
    $sql_results[$key]->users_id = $new_value;
}
return $sql_results;


来源:https://stackoverflow.com/questions/24209085/get-user-metadata-and-display-in-wp-list-table

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!