• cladif

    (@cladif)


    Hello, I would like to insert “Last Viewed Post” in my home using cookies, but I really can’t understand how to do it. Unfortunately I’m new and I don’t have many skills. I hope someone can show me the right way on how to do this. Thank you

    • This topic was modified 5 years ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 5 replies - 1 through 5 (of 5 total)
  • a2hostingrj

    (@a2hostingrj)

    You can show a widget with last viewed posts by a visitor with this plugin:

    https://www.ads-software.com/plugins/posts-viewed-recently/

    Thread Starter cladif

    (@cladif)

    Unfortunately this plugin doesn’t show pictures for me, but what I’d like to understand how to get the “last viewed” function without any plugins.

    a2hostingrj

    (@a2hostingrj)

    how to get the “last viewed” function without any plugins.

    You can take a look at the plugin’s source code. Set a meta value when a post is visited using this way

    if( is_user_logged_in() ) {
    
        update_post_meta( $post_id, 'post_readed_by', get_current_user_id() );
    }

    With that, you can play a little bit with it and get viewed posts:

    <?php 
    $args = array(
        'posts_per_page'   => 10,
        'meta_key'         => 'post_readed_by',
        'meta_value'       => get_current_user_id(),
        'post_type'        => 'post',
        'post_status'      => 'publish',
    );
    $posts_array = get_posts( $args ); ?> 

    Finally, display the posts:

    foreach ( $posts_array as $post ) : setup_postdata( $post );
    
        the_title();
        if ( has_post_thumbnail() ) {
            the_post_thumbnail();
        } 
        the_content();
    
    endforeach; 
    wp_reset_postdata();

    Hope that helps

    Thread Starter cladif

    (@cladif)

    First of all I would like to thank you very much for your availability!

    In this way I can show “last seen posts” only for registered users on my site, right? I would need to show last posts seen to any user of the site, even unregistered.

    I tried to achieve this through cookies, using the “setcookie” function in my post.php file and it seems to register cookies.

    this is the cookie I set:
    setcookie(“recent_views”, $taxonomy_profile, time()+3600); /* expire in 1 hour */

    the problem I have now is that I don’t know how to show results :/

    a2hostingrj

    (@a2hostingrj)

    this is the cookie I set:
    setcookie(“recent_views”, $taxonomy_profile, time()+3600); /* expire in 1 hour */

    the problem I have now is that I don’t know how to show results :/

    Take a look at this post, it might help you with not logged in users:
    https://stackoverflow.com/questions/39483635/show-recently-viewed-products-for-non-logged-in-users-wordpress

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Last Viewed Post’ is closed to new replies.