How do I get how long since current user’s last published post? For example:
It has been <?php echo $days_since_last_post; ?> days since your last post
I need to use that number in a variable for a plugin I am developing.Â
Iâm looking for a solution with performance in mind.
You can use a modified version of this answer by @s_ha_dum.
Where instead of an option you can setup a user meta (even if meta query is slower than option query)
In this way you save in user meta the timestamp of the last edit.
Now you can write a function to return the days since last edit
function return
false
if no user is logged and return-1
if user has never published a post.The query performed is simpler, there is less work PHP does and also result is cached.
This will only be in effect for users after they publish a new post, so all users will start blank, but you can write a function that run only once to setup all user meta.
This function run only once when you go on backend (to avoid frontend slow down).
This is a very slow function so after added in
functions.php
first time you open your dashboard it will take some seconds…This function run once, but probably is better if you remove (or comment) it after it has done its work.
Sounds like you are looking for
get_most_recent_post_of_user()
http://codex.wordpress.org/Function_Reference/get_most_recent_post_of_userIn your theme’s function.php
Then, use:
It has been <?php echo days_since_last_post(); ?> days since your last post