I have created a function that allows users to mark a custom post type in wordpress as viewed. When they mark it, the post_id they have viewed is noted in wp_usermeta in the db.
Here is the code that marks the post as viewed:
function lesson_watched() {
$nonce = $_POST['nonce'];
if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) )
die ( 'Busted!');
if(isset($_POST['lesson_watched']))
{
$user_id = get_current_user_id();
$post_id = $_POST['post_id'];
if(!hasAlreadyWatched($post_id, $user_id))
update_user_meta($user_id, 'has_watched_lesson', $post_id);
else if(hasAlreadyWatched($post_id, $user_id))
delete_user_meta($user_id, 'has_watched_lesson', $post_id);
}
exit;
}
function hasAlreadyWatched($post_id) {
$user_id = get_current_user_id();
if(get_user_meta($user_id, 'has_watched_lesson', $post_id)!='') {
return true;
} else {
return false;
}
}
I have created a function to check if a post is marked as viewed and returns true or false:
function is_lesson_watched() {
$member = MS_Plugin::$api->get_current_member();
// if user has membership AND has watched the video
if(hasAlreadyWatched($post_id) && $member->has_membership()) {
return true;
}
return false;
}
On the front end of my site, I have a loop that shows all posts from a custom post type. I am trying to add a span to all posts that are marked as viewed by using the following code:
<?php if(is_lesson_watched()) {
echo '<span class="fa fa-eye watched_lesson_stamp"></span>';
} ?>
Note: this code is placed within a loop. My issue is that this code seems to add the span to all listed posts in the post type - not the ones that are viewed.
Aucun commentaire:
Enregistrer un commentaire