admin管理员组

文章数量:1417579

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.

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.

Share Improve this question asked Aug 3, 2019 at 20:00 Arg GeoArg Geo 4591 gold badge8 silver badges20 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You're calling get_post_meta() in two different ways: the first, leaving $single unset (it defaults to false) will get you the full array of meta entries for the given key; the second, setting $single to true, will get only the first item with that key.

What you want, I believe, is an array of user IDs attached to each post in the post_is_read meta.

Here's how I'd attack the problem:

single page template

// Get the list of readers.
$readers = get_post_meta( get_the_ID(), 'post_is_read', true );
// If the value is empty, make sure it's an array.
if ( empty( $readers ) ) {
    $readers = array();
}
$current_user = wp_get_current_user();
if ( ! in_array( $current_user->ID, $readers ) ) {
   // Adds the current user ID to the $readers array.
   $readers[] = $current_user->ID;
   // Updates the 'post_is_read' meta.
   update_post_meta( get_the_ID(), 'post_is_read', $readers );
}

posts loop

$readers = get_post_meta( get_the_ID(), 'post_is_read', true );
if ( empty( $readers ) ) {
    $readers = array();
}
$current_user = wp_get_current_user();
if ( ! in_array( $current_user->ID, $readers ) ) {
   // If the current user hasn't read the post, make a note of it.
   echo 'unread post';
}

本文标签: post metagetpostmetaupdatepostmeta array