Array handling with post_meta
-
Hi all,
I’m trying to write some functionality that allows users to ‘favourite’ a post, so the way I was thinking was to create custom post_meta, which holds an array of users that have ‘favourited’ the post. They click a star and if the star is clicked, their user ID is stored in the array, and if they unfavourite, then their user ID is removed from the array. Doing reading of PHP docs I have attempted to use array_push.
The code I have written is as follows:
//define post variables $starred_issue_ID = $_REQUEST['post_id']; $starred_user_ID = $_REQUEST['starred_user_ID']; $starred_new_value = $_REQUEST['starred_new_value']; $starred_users = get_post_meta($starred_issue_ID,'issue_starred'); if (($starred_new_value == 1) and ($starred_user_ID != in_array($starred_users))) { $starred_users_new = array_push($starred_users,$starred_user_ID); } //begin update process update_post_meta($starred_issue_ID, "issue_starred", $starred_users_new);However whenever I do this the result is as follows:
Array ( [0] => 2 ). And no matter which user I use to favourite the post with, it will come up with that. Does anyone have the correct way of doing this?? Once I work this out I also need to work out how to detect if a user has already favourited and if they have and want to unfavourite, remove their user ID from the array….
Ideas???
The topic ‘Array handling with post_meta’ is closed to new replies.