0

Got the below array result from the get_user_meta() WordPress hook

Review the array

Need to check value of all val key whether its value is 1 or not.

The get_users() array have the values :

Array
(
[0] => WP_User Object
    (
        [data] => stdClass Object
            (
                [ID] => 1
                [user_email] => test@test.com
            )


    )

[1] => WP_User Object
    (
        [data] => stdClass Object
            (
                [ID] => 2
                [user_email] => abc@xyz.com
            )


    )
)

get_user_meta() array values:

Array
(
[0] => Array
    (
        [first_name] => Array
            (
                [0] => john
            )

        [val] => Array
            (
                [0] => 0
            )

    )

[1] => Array
    (
        [first_name] => Array
            (
                [0] => mark
            )

        [val] => Array
            (
                [0] => 1
            )

    )

)

Need to take value of email from get_users() where get_user_meta() val == 1

I have tried this below solution but can can not get result.

$is_val = array_search(1,array_column($users,'val'));

if($is_val == 1) { wp_mail($to,$subject,$message,); }

1 Answer 1

1

You can use meta_key or meta_query to fetch users where get_user_meta() 'val' == 1

Option 1:

$users = get_users(array(
    'meta_key'     => 'val',  // your_meta_key
    'meta_value'   => '1',   // value you want to compare
    'meta_compare' => '=',
));

Option 2:

$args = array(
    'meta_query'   =>array(
        array(
            'key' => 'val',  //your_meta_key
            'value' => 1,    // value of key to compare
            'compare' => "="
        ),
    )
);
$users = get_users( $args );

You can run through the loop to get email address of user and send email.

2
  • Can not get what you are trying to express...
    – TMA
    Jun 16, 2017 at 5:10
  • Simply use meta_query parameter inside get_users function to fetch users. It will return users only having get_user_meta() val == 1 Jun 16, 2017 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.