Get user meta for only the keys with the prefix my_theme

Question

There is no native WordPress function to achieve this, but you can get all meta keys for a given user ID:

$all_meta = get_user_meta($user_id);

As you can see in the get_user_meta() docs, since we are leaving the $key argument blank, this will contain an array with all the meta entries for the given user, including the ones you are looking for.

Then you may filter the resulting array with the PHP array_filter() function.

$only_my_theme_meta = array_filter($all_meta, function($key){
    return strpos($key, 'my_theme') === 0;
}, ARRAY_FILTER_USE_KEY);

0
Mitterrand Nkulu 1 month 2021-05-09T00:41:41-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse