There are some values in Post Meta Data stored as a multi dimensional array. I want to update some of their data.
Here is the Post Meta value displayed using <?php the_meta(); ?>
voter: a:1:{s:5:"voter";a:5:
{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.4
8.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}},
a:1:{s:5:"voter";a:5:
{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48
.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}},
a:1:{s:5:"voter";a:5:
{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:2:"10";s:8:"voter_ip";s:13:"182.48.
238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}}
Now I want to update the vote
or voter_ip
where user_id
is 832 or 1540. I tried using update_post_meta()
but its updating everything.
So how to update the post meta of single value stored in multi dimension array?
Update:
array using the-meta()
voter: a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}},
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}, , ,
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}},
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}
array using print_r()
Array( [0] => Array ( [voter] => Array ( [post_id] => 219585 [voter_id] =>
832 [voter_ip] => 182.48.238.86 [author_id] => 1540 [vote] => 1 ) ) [1] =>
Array ( [voter] => Array ( [post_id] => 219585 [voter_id] => 1540 [voter_ip]
=> 182.48.238.86 [author_id] => 1540 [vote] => -1 ) ) [2] => [3] => [4] =>
Array ( [voter] => Array ( [post_id] => 219585 [voter_id] => 832 [voter_ip]
=> 182.48.238.86 [author_id] => 1540 [vote] => 1 ) ) [5] => Array ( [voter]
=> Array ( [post_id] => 219585 [voter_id] => 1540 [voter_ip] =>
182.48.238.86 [author_id] => 1540 [vote] => -1 ) ))
There are some values in Post Meta Data stored as a multi dimensional array. I want to update some of their data.
Here is the Post Meta value displayed using <?php the_meta(); ?>
voter: a:1:{s:5:"voter";a:5:
{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.4
8.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}},
a:1:{s:5:"voter";a:5:
{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48
.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}},
a:1:{s:5:"voter";a:5:
{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:2:"10";s:8:"voter_ip";s:13:"182.48.
238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}}
Now I want to update the vote
or voter_ip
where user_id
is 832 or 1540. I tried using update_post_meta()
but its updating everything.
So how to update the post meta of single value stored in multi dimension array?
Update:
array using the-meta()
voter: a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}},
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}, , ,
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:3:"832";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:1:"1";}},
a:1:{s:5:"voter";a:5:{s:7:"post_id";s:6:"219585";s:8:"voter_id";s:4:"1540";s:8:"voter_ip";s:13:"182.48.238.86";s:9:"author_id";s:4:"1540";s:4:"vote";s:2:"-1";}}
array using print_r()
Array( [0] => Array ( [voter] => Array ( [post_id] => 219585 [voter_id] =>
832 [voter_ip] => 182.48.238.86 [author_id] => 1540 [vote] => 1 ) ) [1] =>
Array ( [voter] => Array ( [post_id] => 219585 [voter_id] => 1540 [voter_ip]
=> 182.48.238.86 [author_id] => 1540 [vote] => -1 ) ) [2] => [3] => [4] =>
Array ( [voter] => Array ( [post_id] => 219585 [voter_id] => 832 [voter_ip]
=> 182.48.238.86 [author_id] => 1540 [vote] => 1 ) ) [5] => Array ( [voter]
=> Array ( [post_id] => 219585 [voter_id] => 1540 [voter_ip] =>
182.48.238.86 [author_id] => 1540 [vote] => -1 ) ))
Share
Improve this question
edited Jul 24, 2016 at 15:15
Ramesh Pardhi
asked Jul 24, 2016 at 9:08
Ramesh PardhiRamesh Pardhi
8032 gold badges7 silver badges14 bronze badges
1 Answer
Reset to default 1You may need to unserialize the data to get the array and loop through it thus:
$userid = 832; // or 1540
$votes = get_post_meta($postid,'voter');
$votes = maybe_unserialize($votes);
if (is_array($votes)) {
// votes is the array, key is numeric index, vote is subarray
foreach ($votes as $key => $vote) {
// subarray values are in another array with key 'voter'
if ($vote['voter']['voter_id'] == $userid) {
$votes[$key]['voter']['vote'] = $newvote;
$votes[$key]['voter']['voter_ip'] = $newvoterip;
}
}
update_post_meta($postid,'voter',$voter);
}