2

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.

1
  • Supposed to say “It has been <?php echo $days_since_last_post; ?> days since your last post”...
    – Mark
    Commented Nov 12, 2013 at 16:38

2 Answers 2

5

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.

5
  • Great!! Question, if I add this now to my site, it will only be in effect for users after they publish a new post? So all users will start blank?
    – Mark
    Commented Nov 12, 2013 at 17:23
  • What type of drink do I need to share with you... for you to give me this magical function? ;)
    – Mark
    Commented Nov 12, 2013 at 17:29
  • 1
    You are a king. tested it and everything is in order! thank you so much!!
    – Mark
    Commented Nov 12, 2013 at 17:58
  • 2
    I've been using this code as well but it has one problem. It updates the current_user's meta value, not the post-author's. So if an admin/editor publishes the post, the post-author, who had their post pending will not have their value updated. To fix this, on "update_user_meta()", simply replace "get_current_user_id()" with "$p->post_author".
    – Christine Cooper
    Commented Nov 21, 2013 at 17:51
  • 1
    @ChristineCooper in that case, if the update is done by another user, using your suggestion last update can be, let's say, today, even if that user don't update a post since a long time. Maybe the last_update meta should be updated only if $p->post_author == get_current_user_id(). Code as is target "last time an user update a post (no matter the post author)". Your code target "Last time a post by a specific user was updated".
    – gmazzap
    Commented Nov 22, 2013 at 13:08
2

Sounds like you are looking for get_most_recent_post_of_user() http://codex.wordpress.org/Function_Reference/get_most_recent_post_of_user

In your theme's function.php

    days_since_last_post(){
        $recent = get_most_recent_post_of_user( get_current_user_id() );
        $start = strtotime($recent['post_date_gmt']);
        return ceil(abs(time() - $start) / 86400);
    }

Then, use:

It has been <?php echo days_since_last_post(); ?> days since your last post

5
  • Is this the best solution for performance?
    – Mark
    Commented Nov 12, 2013 at 17:08
  • @Mark probably not...
    – gmazzap
    Commented Nov 12, 2013 at 17:09
  • @G. M. Can you please post a better alternative with performance in mind?
    – Mark
    Commented Nov 12, 2013 at 17:12
  • This isn't that inefficient. Even the most optimized code would require get_current_user_id(), a database call, and math to find difference between today and the date returned. Commented Nov 12, 2013 at 17:20
  • Note: this will only work on Multisite
    – Tim Malone
    Commented Jun 23, 2016 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.