I have stored user favorite posts in the user_meta table of WordPress. The data is stored in a format, for example a:1:{s:8:"post-134";s:3:"134";s:8:"post-136";s:3:"136";}.

How can I get this in the form of an array like array( 134, 136 ) so that I can use it in the query below.

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query( array( 
    'paged' => $paged, 
    'post_type' => 'post', 
    'post__in' => I WANT AN ARRAY HERE 
) );
if ( $query->have_posts() ) : 
    while ( $query->have_posts() ) : $query->the_post();
        get_template_part( 'content', 'fav' )
    wp_reset_postdata(); else :
        ?><div class="alert alert-warning">You have not faved any post.</div><?php

2 Answers 2


Take a look at PHP's array_values function. Alternatively, you could typecast.

Note that if the array is in serialized form as you have put it above, you will have to unserialize it first, in either case.

$numerical_array = array_values(


$numerical_array = (array) unserialize(
  • I'm sorry. I did not understand. I did it like this. $favpsts = get_user_option( "_fav_posts", $current_user->ID ); $fp_array = (array) unserialize($favpsts); but it is not working.
    – Sam
    Sep 13, 2014 at 15:31
  • 1
    For one, typecasting might have been a bad idea, since the result will probably remain associative. For another, the sample you posted is a broken serialization, since an array with two elements should start with a:2 in serialized form - that however is out of scope of this question. Anyhow, use array_values(). Sep 13, 2014 at 18:36

You can get the data from user meta with the below code

$pastids = get_user_meta($user_id, $key);

$pastids will be an array automatically. Now we can change its structure in any format.

if we pass an array to add_user_meta() it will save it in serialized format and when we get it using get_user_meta() it return save data in array format.

Your Answer

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

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