本文介绍了获取用户元数据并显示在 WP_List_Table 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这种情况下,我正在尝试获取用户元数据,我将从 WP_List_Table 类中找到用户名或用户名或姓氏.由于我有一个名为 users_id 的字段,我在其中存储了 wp_users.ID 值,因此我在函数 get_sql_results() 中执行了此操作:

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;
}

所以我得到了users_id.然后在函数 get_columns() 中我显示的值如下:

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;
}

但是当我应该显示用户名或名字或姓氏时会显示用户 ID,这就是我怀疑的地方,我如何使用 get_user_metadata($user_id) 函数获取值和显示在结果表中?

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?

推荐答案

您必须迭代到 $sql_results 并将 users_id 替换为您自定义的 get_user_metadata($users_id).类似的东西:

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;

这篇关于获取用户元数据并显示在 WP_List_Table 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 03:10