I’ve been trying to look for a way to create a loop in my website and show posts being read/watched but have not found anything.
The only way I can accomplish this would be to create a new meta key (custom field) and assign timestamp as value, that way I can create a loop and use its value to list posts that are being watched within a time range, say 5 minutes.
The issue is that I am very very new to wordpress and i dont know how to do this T_T
A very simplistic method of achieving this would be as follows;
Note: The above function goes into your theme
functions.php
file and is hooked onto thetemplate_redirect
action which is fired just before displaying the page to the user. At this point, behind the scenes, ourrecently_read
function will fire and add a time stamp under the keyreading_this
(name it to your liking). This will occur each time this or any post is viewed. If a time stamp already exists for a given post, it will then update the time stamp accordingly.Then to retrieve a list of posts with this key you would do the following in your theme files where you want those posts to display;
The above could be improved a number of ways but this will definitely get you started.
Note: This is void of any logic that determines if the post was viewed in the last X minutes. For that you can apply your own logic to the meta_key named
reading_this
and manipulate the data to your liking.