Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have an array saved in postmata, each array key become a metakey. I want to change the code to save the whole array with one metakey. How to do that? Thanks!

$poddata = Array(
'pod_id' => $this->pod_id,
'url' => $this->url,
'name' => $this->name,
'description' => $this->description,
'service' => $this->service,
'status' =>$this->status,
'price' => $this->price
);

foreach ( $poddata as $k => $v ){

if ( get_post_meta( $this->id, $k ) == '' )
add_post_meta( $this->id, $meta_box, $v, true );

elseif ( $v != get_post_meta( $this->id, $k, true ) )
update_post_meta( $this->id, $k, $v );

elseif ( $v == '' )
delete_post_meta( $this->id, $k, get_post_meta( $this->id, $k, true ) );

}
share|improve this question

1 Answer 1

up vote 7 down vote accepted

you don't need to loop through the values. Just use update_post_meta($post_ID, {key}, {array of vals}), it would do!

<?php
$poddata = Array(
    'pod_id' => $this->pod_id,
    'url' => $this->url,
    'name' => $this->name,
    'description' => $this->description,
    'service' => $this->service,
    'status' =>$this->status,
    'price' => $this->price
    );

//Update inserts a new entry if it doesn't exist, updates otherwise
update_post_meta($post_ID, 'poddata', $poddata);
?>

Thats it! When you fetch it for usage, do the following:

    $poddata = get_post_meta($post_ID, 'poddata');

$poddata is the array of values.

share|improve this answer
    
I tried update_post_meta($post_ID, 'poddata', $postdata), after save the post, i see the meta is not saved. –  Jenny Aug 6 '11 at 8:49
    
oh, sorry, It saved, I didn't see it on WP Custom Fields Table. I just found it in phpAdmin. Thanks! –  Jenny Aug 6 '11 at 8:59
    
When retrive the data, by get_post_meta($post_ID, 'poddata'); I get array(0) from var_dump. How can I get the whole array? –  Jenny Aug 6 '11 at 9:28
    
You're welcome! Try using print_r()... echo "<pre>"; print_r($poddata); echo "</pre>"; –  Rutwick Gangurde Aug 6 '11 at 9:30
    
print_r($poddata) shows Array() –  Jenny Aug 6 '11 at 9:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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