get_most_recent_post_of_user

Get a user's most recent post.

Description

(array) get_most_recent_post_of_user( (int) $user_id ); 

Walks through each of a user's blogs to find the post with the most recent post_date_gmt.

Returns (array)

Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts

Parameters (1)

0. $user_id (int)
The user id.

Usage

  1. if ( !function_exists( 'get_most_recent_post_of_user' ) ) { 
  2. require_once ABSPATH . WPINC . '/ms-functions.php'; 
  3.  
  4. // The user id. 
  5. $user_id = -1; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_most_recent_post_of_user($user_id); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/ms-functions.php  
  1. function get_most_recent_post_of_user( $user_id ) { 
  2. global $wpdb; 
  3.  
  4. $user_blogs = get_blogs_of_user( (int) $user_id ); 
  5. $most_recent_post = array(); 
  6.  
  7. // Walk through each blog and get the most recent post 
  8. // published by $user_id 
  9. foreach ( (array) $user_blogs as $blog ) { 
  10. $prefix = $wpdb->get_blog_prefix( $blog->userblog_id ); 
  11. $recent_post = $wpdb->get_row( $wpdb->prepare("SELECT ID, post_date_gmt FROM {$prefix}posts WHERE post_author = %d AND post_type = 'post' AND post_status = 'publish' ORDER BY post_date_gmt DESC LIMIT 1", $user_id ), ARRAY_A); 
  12.  
  13. // Make sure we found a post 
  14. if ( isset($recent_post['ID']) ) { 
  15. $post_gmt_ts = strtotime($recent_post['post_date_gmt']); 
  16.  
  17. // If this is the first post checked or if this post is 
  18. // newer than the current recent post, make it the new 
  19. // most recent post. 
  20. if ( !isset($most_recent_post['post_gmt_ts']) || ( $post_gmt_ts > $most_recent_post['post_gmt_ts'] ) ) { 
  21. $most_recent_post = array( 
  22. 'blog_id' => $blog->userblog_id,  
  23. 'post_id' => $recent_post['ID'],  
  24. 'post_date_gmt' => $recent_post['post_date_gmt'],  
  25. 'post_gmt_ts' => $post_gmt_ts 
  26. ); 
  27.  
  28. return $most_recent_post;