You can show a widget with last viewed posts by a visitor with this plugin:
https://wordpress.org/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.
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 :/
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