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)
add_action('save_post', 'user_last_update', 10, 2);
function user_last_update($id, $p) {
if (
(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
|| (defined('DOING_AJAX') && DOING_AJAX)
|| ($p->post_status !== 'publish')
) {
return;
}
update_user_meta( get_current_user_id(), 'last_update', time() );
}
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 user_last_edit(){
$id = get_current_user_id();
if ( ! $id ) return false;
$recent = get_user_meta( $id, 'last_update', true );
return $recent ? round( abs( time() - $recent ) / DAY_IN_SECONDS ) : -1;
}
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.
function setup_last_edit() {
if ( get_transient('setup_last_edit_done') ) return; // this function run once
$users = get_users( array('fields'=> 'ID') );
foreach ( $users as $userid ) {
$args = array(
'author' => $userid,
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC'
);
$posts = get_posts($args);
if ( empty($posts) ) continue;
$p = array_pop($posts);
$last = mysql2date('U', $p->post_modified );
update_user_meta( $userid, 'last_update', $last );
}
set_transient('setup_last_edit_done', 1);
}
add_action('admin_init', 'setup_last_edit');
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.