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 ) );
}
You don’t need to loop through the values. Just use update_post_meta($post_ID, {key}, {array of vals})
, it should 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.