下面是一个获取用户ID的示例,循环遍历每个ID,获取一个meta\\u键作为数组,向其中添加新值并删除旧值,保存回meta,然后再次获取它以循环遍历数组中的每个项以进行输出。数组只是重复键的一个示例。
$user_query = new WP_User_Query( array( \'fields\' => \'ID\' ));
$user_ids = $user_query->get_results();
echo "<ul>";
foreach ( $user_ids as $user_id ) {
// all meta for user
$all_meta_for_user = array_map( function( $a ){ return $a[0]; }, get_user_meta( $user_id ) );
echo "<pre>";
print_r ( $all_meta_for_user ); // see all the values
echo "</pre>";
$key = \'__TEST_KEY\';
$value_set = array();
if( isset( $all_meta_for_user [ $key ] )) {
$value_set = get_user_meta ( $user_id, $key, true );
if( ! is_array( $value_set )) $value_set = array(); // make sure we\'re working with an array
}
// add to existing values
$value_set[] = timer_stop(4);
// remove old items
if( count ($value_set) > 4 ) array_shift ($value_set);
// set the meta
update_user_meta($user_id, $key, $value_set );
// LATEST VALUES
$value_set = get_user_meta ( $user_id, $key, true );
echo "<pre>";
print_r ( $value_set ); // the value set currently in our meta
echo "</pre>";
foreach ( $value_set as $inx => $value ){
echo "<li>{$user_id} - {$key} - {$inx} - {$value}</li>";
}
echo "</ul>";
}
echo "</ul>";