0

I have a loop that is working great and doing exactly what I want. The problem is that I need to exclude specific posts that have already been marked as viewed using add_user_meta. Each post is marked as viewed using update_user_meta and the post id with the value '1'.

$_goal = $_GET['goal'] != '' ? $_GET['goal'] : '';
$calmargs = array('post_type'=>'yoga_post','goal'=>$_goal,'orderby'=>'date','order'=>'DESC','posts_per_page'=>1);
$calm = new WP_Query($calmargs);
if ($calm->have_posts()) { 
    while($calm->have_posts()) : $calm->the_post(); ?>

        <div>
        <div>   
            <a href="https://members.com/?det=<?php echo get_the_ID(); ?>" target="_blank"><?php the_post_thumbnail(); ?></a>
            <h4 style="height:70px;padding-top:15px"><a href="https://member.com/?det=<?php echo get_the_ID(); ?>" target="_blank" title="<?php the_title(); ?>"><?php the_title(); ?></a><h4>

        <?php
            $yogavid = get_the_ID('yoga_post');     
            $videowatched = get_user_meta($current_user->ID, 'video_viewed_' . $yogavid, true);
            $one = '1';
            $zero='0';

            if ($videowatched != '1'){ ?>
                <form method="POST" target="_self" action="">
                    <input type="submit" class="watched-vid" id="<?php echo ($one.$yogavid); ?>" name="<?php echo ($one.$yogavid); ?>" value="Mark as Viewed">
                </form>
                <?php if(isset($_POST[($one.$yogavid)])){
                    update_user_meta($current_user->ID,'video_viewed_' . $yogavid, '1'); ?>
                    <script>
                    jQuery('#<?php echo ($one.$yogavid); ?>').attr( 'value' , 'Viewed' );
                    </script>
                <?php }  
            } else { ?>
                <form method="POST" target="_self" action="">
                    <input type="submit" class="watched-vid" id="<?php echo ($zero.$yogavid); ?>" name="<?php echo ($zero.$yogavid); ?>" value="Viewed">
                </form>
                <?php if(isset($_POST[($zero.$yogavid)])){
                    update_user_meta($current_user->ID,'video_viewed_' . $yogavid, '0'); ?>
                    <script>
                    jQuery('#<?php echo ($zero.$yogavid); ?>').attr( 'value' , 'Mark as Viewed' );
                    </script>   
                <?php } 
            } ?>    
            <?php 
                $calmpost = get_the_ID('yoga_post');
                add_user_meta($user_id,'calm_post', $calmpost, true); ?>
        </div>
    </div>
<?php 
endwhile; 

I need to exclude the posts with the value '1' in key 'video_viewed_' . $yogavid . I've tried nested loops and pre_get_posts but am coming up empty. Would appreciate any advice on how to get this working!

1 Answer 1

0

A simpler approach would be to store a list/array of post-ids of the posts which have been marked read by the user in his user_meta. Then retrieve this array for every logged-in user, and run the wp_query using the post__not_in parameter with the array as the argument.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.