Save array to post meta

For example, you need to save list of users, who entered each page or post:

<?php
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);  
}
?>
VN:F [1.9.17_1161]
Rating: 0 (from 0 votes)

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">