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 badges1 Answer
Reset to default 1You'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
版权声明:本文标题:post meta - get_post_metaupdate_post_meta array 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745272747a2650993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论