For example, you need to save list of users, who entered each page or post:
global $post, $current_user; $data = unserialize(get_post_meta($post->ID, '_list', true)); if( count($data) != 0 ) { if ( !in_array( $current_user->ID, $data ) ) { $data[] = $current_user->ID; } $data = array_unique($data); // remove duplicates sort( $data ); // sort array //$data = serialize($data); update_post_meta($post->ID, '_list', $data); } else { $data = array(); $data[0] = $current_user->ID; //$data = serialize($data); update_post_meta($post->ID, '_list', $data); }
Great, this really helps!
It doesn't work with the latest WordPress however. This line in both blocks isn't necessary:
$data = serialize($data);
In fact it will store a serialized string containing the serialized array in it. WordPress automatically serializes arrays when update_post_meta is called.
Great! very helpfull... thanks!