A WordPress question. Thanks for your consideration.

Isn't it possible to specify array_column information in meta_query key specification?

Learn more

The article has a custom field called premium_users.

The contents are the following array.

$premium_users = get_post_meta ($post_id, 'premium_users');
/ *
Here's what $premium_users looks like
  ['user_id' =>1, 'age' =>10],
  ['user_id' =>2, 'age' =>20]

I would like to use this information in a meta_query search as shown below.

While the specified value in meta_query is a user ID as shown below, the target array was stumbled with information called age as shown above.

How can I specify meta_query in such cases?

'meta_query' = array (
        array (
              'value' =>get_current_user_id () // I want to specify by user ID
            , 'key' =>'premium_users', // but here is age
            , 'type' =>'NUMERIC'
            , 'compare' =>'LIKE'
  • Answer # 1

    'meta_query' = array (
            array (
                  'value' =>'%'. substr (serialize (['user_id' =>get_current_user_id ()]), 5, -1). '%'
                , 'key' =>'premium_users'
                , 'type' =>'NUMERIC'
                , 'compare' =>'LIKE'

    If you are using a custom field plug-in
    It might be JSON, not serialized
    user_id value type may be string instead of int
    Check it by looking in the DB post_meta table

Related articles