OK – so, first, you don’t need to serialize/unserialize. get_user_meta()
will return an array, and delete_user_meta()
can remove a specific value.
So, simplifying a few other things here and below, your remove function would work like this (very simple):
function teo_bookmark_remove() {
$postID = $_POST['post_id'];
$current_user = get_current_user_id();
delete_user_meta( $current_user, 'bookmarks', $postID );
}
Likewise bookmarks submit: Very simple (though probably here and in the previous function you should be using nonces or other security features- not sure about how rest of the set-up is handling the matter):
function teo_bookmark_submit() {
// get the submitted parameters
$postID = $_POST['post_id'];
$current_user = get_current_user_id();
update_user_meta( $current_user, 'bookmarks', $postID );
}
Now the display function:
if (!is_user_logged_in() ) {
?>
<div class="side-article">
<h2><?php _e('Please login if you want to see bookmarked posts!', 'Aruna');?></h2>
</div>
<?php } else {
$current_user = get_current_user_id();
$bookmarks = get_user_meta($current_user, 'bookmarks'); //returns array
if ( ! empty($bookmarks) ) {
$args = array();
$args['post_type'] = 'post';
$args['post__in'] = $bookmarks; //post__in takes array
$args['order'] = 'DESC';
$args['posts_per_page'] = 8;
//performance improvements
$args['no_found_rows'] = true;
$args['update_post_term_cache'] = true;
$args['update_post_meta_cache'] = true;
$paged = is_front_page() ? get_query_var( 'page' ) : get_query_var( 'paged' );
$args['paged'] = $paged;
$wp_query = new WP_Query($args);
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) : $wp_query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
wp_reset_postdata();
get_template_part('includes/pagination');
}
} else { ?>
<div>
<h2><?php _e("Bummer, no bookmarks posts!", 'Aruna');?></h2>
</div>
<?php }
}
Tested the “no bookmarks” part – the part that was giving you problems. Can’t vouch completely for the results of the $bookmarks not empty alternative, of course, but works with a simple is_empty test.
Note: I believe on the above functions, the same value may end up appearing multiple times in the ‘bookmarks’ array. I don’t think it’s a problem for post__in or delete_post_meta (will delete all instances of particular value associated with the key), but may be if you use the ‘bookmarks’ array for other purposes. Not hard hard to fix that if you need to.