I am trying to save the ID of the current user (if user is logged in) within an array in a post_meta key called "post_is_read" in every post the user reads so I can perform certain actions such as label the post as "unread".
I added this in single page template:
$readers = get_post_meta(get_the_ID(), 'post_is_read');
$current_user = wp_get_current_user();
if (!in_array($current_user->ID, $readers)) {
$readers[] = $current_user->ID;
update_post_meta( get_the_ID(), 'post_is_read', $readers, false);
}
Inside the posts loop I added the following code:
$readMeta = get_post_meta( get_the_ID(), 'post_is_read', true );
$current_user = wp_get_current_user();
if ( $readMeta != $current_user->ID ) {
echo 'unread post';
}
I tried many variations of the first block of code, altered add_post_meta, update_post_meta etc. One problem I recognized is when printing the array stored in the meta key "post_is_read" the output is just "array". I think this is a major problem if it is not the only one in the code.