获取用户元数据并在WP_List_Table中显示

时间:2014-06-13 15:35:35

标签: php wordpress wordpress-plugin

我试图在这种情况下获得用户metada我将在WP_List_Table类中找到用户名或用户名或姓。由于我有一个名为users_id的字段,我存储wp_users.ID值,然后我在函数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()中,我显示如下值:

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)函数得到值并显示在结果表中?< / p>

1 个答案:

答案 0 :(得分:1)

您必须迭代$sql_results并使用自定义users_id替换get_user_metadata($users_id)。类似的东西:

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;