Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Liam Maclachlan](https://secure.gravatar.com/avatar/1ba6ebda302de5c767ae76b817b12cc2?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Liam Maclachlan
22,805 PointsWhy does my meta data keep nesting old array items when adding new ones?
So... I am near going to find a new way of doing this as no one seems to have an answer.
I'm trying to add a new item to an array that is stored in a posts meta data.
The first will retun like this:
<?php
Array
(
[0] => The first commit // first action
)
?>
The second then returns this!
<?php
Array
(
[0] => Array
(
[0] => The first commit // first action
[1] => Array // second action
(
[0] => Page was updated by userx (User X)
)
)
)
?>
And one last for examples sake...
<?php
Array
(
[0] => Array
(
[0] => Array
(
[0] => The first commit // first action
[1] => Array // second action (
[0] => Page was updated by userx (User X)
)
)
[1] => Array // thrid action
(
[0] => Page was deleted and sent to the trash by userx (User X)
)
)
)
?>
Does anyone have ananswer for this!? Very frustrating...
This is my code that generates the array:
<?php
$log = get_post_meta($post_id, 'audit_log'); ## retrieve the log, first
$message = array(); ## create message array
if ( current_filter() == 'before_delete_post' ) : ## if hook 'before_delete_post' was called
$message[] = 'Page was deleted permenantly from the trash by ' . $updater_info; ## message to be parsed in to meta
elseif ( $the_post->post_status == 'trash') : ## if post was sent to trash
$message[] = 'Page was deleted and sent to the trash by ' . $updater_info;
else :
$message[] = 'Page was updated by ' . $updater_info;
endif;
update_post_meta( $post_id, 'last_update', $message); // update last update with the above message
$log[] = $message;
update_post_meta( $post_id, 'audit_log', $log );
?>
EDIT: I'm typing this out and realising I'm beeing an idiot... I'm gonna leave this here anyway incase I'm wrong, or anyone else has the same problem.
1 Answer
![Liam Maclachlan](https://secure.gravatar.com/avatar/1ba6ebda302de5c767ae76b817b12cc2?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Liam Maclachlan
22,805 PointsYUUUUUUP.... I'm an idiot. Don't use 'update_post_meta'. Update seems to thow out arrays for fun. If the meta is an array already, just use 'add_post_meta' instead. The new value will be stored at the end of the array. So all you need to do is 'add_post_meta' it with the new value...
Code below:
<?php
if ( !$audit_count = get_post_meta( $newPost, 'audit_log', true ) ) : ## search to see if an audit trail has been previuolsy assigned to the post
$message = 'Page was created by ' . $updater_info; // append the log info the the end of the array
update_post_meta($newPost, 'audit_log', $message);
else :
if ( current_filter() == 'before_delete_post' ) : ## if hook 'before_delete_post' was called
$message = 'Page was deleted permenantly from the trash by ' . $updater_info; ## message to be parsed in to meta
elseif ( $the_post->post_status == 'trash') : ## if post was sent to trash
$message = 'Page was deleted and sent to the trash by ' . $updater_info;
else :
$message = 'Page was updated by ' . $updater_info;
endif;
add_post_meta( $newPost, 'audit_log', $message ); ## this will append the new value to the end of the meta array, instead of adding ANOTHER array :)
?>
Liam Maclachlan
22,805 PointsLiam Maclachlan
22,805 PointsI'm actually wondering if it was when I was using the get_post_meta orginally, it was returning an array and I was adding to that array.... :$
I am going to go back and try that :)
Liam Maclachlan
22,805 PointsLiam Maclachlan
22,805 PointsYup. This is the best solution I can find :)