I'm having a problem retrieving post_meta values that I have stored in an array. I use this for storing the array:

add_action( 'comment_post', 'add_food', 1 );
function add_food(){
    global $post;
    $testvalues = array('bread', 'cake');
    update_post_meta($post->ID, 'food', $testvalues);

and this for retrieving it:

add_filter('the_content', 'print_food');
function print_food($content){
    global $post;
    $custom_fields = get_post_custom($post->ID);
    $food_field = $custom_fields['food'];
    $content .= $food_field[0];
    return $content;

however, this is added to the content of my page:


The technique works fine for the comment_meta. What am I doing wrong here?

  • What do you get if you print_r( $custom_fields );? Have you tried using a foreach loop to print out the results? – mrwweb May 5 '12 at 14:58
  • Thanks for the quick reply! for 'code'print_r( $custom_fields ); I get: – maarten May 5 '12 at 15:33
  • Array ( [average_wr] => Array ( [0] => 83 ) [_edit_last] => Array ( [0] => 1 ) [_edit_lock] => Array ( [0] => 1336231767:1 ) [food] => Array ( [0] => a:2:{i:0;s:5:"bread";i:1;s:4:"cake";} ) ) where 'average_wr' is one of my own variables, and so is 'food'. So I guess something is going wrong with storing the array? – maarten May 5 '12 at 15:33
  • Interesting...I've seen that data format from other plugins that store arrays in custom fields (like Advanced Custom Post types), but I can't say that I'm familiar with what that format is and how to deal with it. Just looking at it, it seems that it's storing "key," "value length," "value" for each item in that weird bracket-y format. – mrwweb May 5 '12 at 15:59
  • BTW, I think the first three lines of your print_food() function can be condensed down to $food_field = get_post_meta( $post->ID, 'food' ); – mrwweb May 5 '12 at 16:03

Aha! I think I found your answer. Two facts:

An array will be serialized into a string.

If set to true then the function will return a single result, as a string. If false, or not set, then the function returns an array of the custom fields. This is not intuitive. For example, if you fetch a serialized array with this method you want $single to be true to actually get an unserialized array back. If you pass in false, or leave it out, you will have an array of one, and the value at index 0 will be the serialized string. (emphasis added)

Therefore, this [untested] snippet should get you your food array as an array rather than a serialized string.

$food_field = get_post_meta( $post->ID, 'food', true );
| improve this answer | |
  • Ah, this works! I now remember having read this when I was first looking into the post meta. I guess I was so blinded by the fact that it worked for my comment_meta, that I missed the solution. – maarten May 5 '12 at 17:13
  • Glad to hear it! If this solved your issue, make sure to mark it as accepted so others can find help on this thread. Feel free to edit this answer if you needed to change anything else. – mrwweb May 5 '12 at 17:29
  • I don't see any button to accept the anser o_O, I also can't upvote the answer, because I don't have 15 reputation yet. Am I missing something? – maarten May 5 '12 at 17:40
  • There should be a check mark you can toggle below the voting buttons. – mrwweb May 5 '12 at 17:47

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.