1

I want to pass user meta_key to meta_value, but the meta_value array is like this:

$user_biodata = array(
    'register_facebook_id' => 1311064406686333,
);

I want check if user is already registered through this function;

$wp_users = get_users(array(
    'number'       => 1,
    'count_total'  => false,
    'fields'       => 'id',

    'meta_query' => array(
        array(
            'meta_key' => 'user_biodata',
            'meta_value' => array( 'register_facebook_id' => 1311064406686333 ),
        )
    ),
));

But the result is always 1 even when the value not exist, how do I fix it?

1

What you are doing is not a valid way of retrieving and checking serialize data from the WP. You may insert array as a user meta but this array behind the scenes is transformed to serialize string something the MySql can handle.

There are options for searching with the LIKE operator but I don't endorse this use as it can lead to unexpected results.

$args = array(
'number'       => 1,
'count_total'  => false,
'fields'       => 'ID',

'meta_query' => array(
        array(
            'key' => 'user_biodata',
            'value' => 'yourValue',
            'compare' => 'LIKE',
        ),

    ),
);

$users = get_users($args);
  • 1
    Thanks, but not using '%%'., – Opsional Nov 6 '17 at 11:18
  • Yes @Opsional you are correct this is redundant. – Drupalizeme Nov 6 '17 at 11: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.