skip to Main Content

I update Many Meta in current user and i want get all of meta

   <?php 
    $new_user_id = 1 ;
    $stats = test;
update_user_meta( get_current_user_id() , 'add-user-id-'.$new_user_id, $stats);

$new_user_id = 2 ;
    $stats = test;
update_user_meta( get_current_user_id() , 'add-user-id-'.$new_user_id, $stats);
 ?>

How can I get Any Meta Like ‘add-user-id-[array of all users id ]’ ?

Example : I want List Of all Users Id saved in current user

2

Answers


  1. From the current user ID you can get all its related metadata with get_user_meta() like:

    // Get all user metadata in an array
    $user_meta = get_user_meta( get_current_user_id(), false, true );
    
    // Display the metadata array
    echo '<pre>'. print_r( $user_meta, true ) . '</pre>';
    
    Login or Signup to reply.
  2. You can get all user_meta and then find the ones you need. For example, something like this:

    $new_user_id = 1 ;
    update_user_meta( get_current_user_id(), "add-user-id-$new_user_id", $new_user_id );
    
    $new_user_id = 2 ;
    update_user_meta( get_current_user_id(), "add-user-id-$new_user_id", $new_user_id );
    
    $find_meta_ids = [1, 2];
    $find_key_prefix = 'add-user-id-';
    
    $output = [];
    $all_meta = get_user_meta( get_current_user_id() );
    
    foreach ( $all_meta as $key => $value ) {
        $key_id = array_pop( explode( $find_key_prefix, $key ) );
        if( in_array( $key_id, $find_meta_ids ) ) {
            $output[$key] = $value[0];
        }
    }
    
    var_dump( $output );
    

    Result: array(2) { ["add-user-id-1"]=> string(1) "1" ["add-user-id-2"]=> string(1) "2" }

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search