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 Answer 1

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);
2
  • 1
    Thanks, but not using '%%'., Commented Nov 6, 2017 at 11:18
  • Yes @Opsional you are correct this is redundant. Commented Nov 6, 2017 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.