get_most_recent_post_of_user

The timeline below displays how wordpress function get_most_recent_post_of_user has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Gets a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU (3.0.0)
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id User ID.
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts.
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    /*
     * Walk through each blog and get the most recent post
     * published by $user_id.
     */
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post.
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            /*
             * If this is the first post checked
             * or if this post is newer than the current recent post,
             * make it the new most recent post.
             */
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 6.2

/**
 * Gets a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU (3.0.0)
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id User ID.
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts.
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id.
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post.
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            /*
             * If this is the first post checked
             * or if this post is newer than the current recent post,
             * make it the new most recent post.
             */
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 6.1

/**
 * Gets a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU (3.0.0)
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id.
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post.
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            /*
             * If this is the first post checked
             * or if this post is newer than the current recent post,
             * make it the new most recent post.
             */
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 5.4

/**
 * Get a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU (3.0.0)
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id.
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post.
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            /*
             * If this is the first post checked
             * or if this post is newer than the current recent post,
             * make it the new most recent post.
             */
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 4.9

/**
 * Get a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU (3.0.0)
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            // If this is the first post checked or if this post is
            // newer than the current recent post, make it the new
            // most recent post.
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 4.4

/**
 * Get a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            // If this is the first post checked or if this post is
            // newer than the current recent post, make it the new
            // most recent post.
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 4.3

/**
 * Get a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU
 *
 * @global wpdb $wpdb
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            // If this is the first post checked or if this post is
            // newer than the current recent post, make it the new
            // most recent post.
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 4.1

/**
 * Get a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            // If this is the first post checked or if this post is
            // newer than the current recent post, make it the new
            // most recent post.
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}

WordPress Version: 3.7

/**
 * Get a user's most recent post.
 *
 * Walks through each of a user's blogs to find the post with
 * the most recent post_date_gmt.
 *
 * @since MU
 * @uses get_blogs_of_user()
 *
 * @param int $user_id
 * @return array Contains the blog_id, post_id, post_date_gmt, and post_gmt_ts
 */
function get_most_recent_post_of_user($user_id)
{
    global $wpdb;
    $user_blogs = get_blogs_of_user((int) $user_id);
    $most_recent_post = array();
    // Walk through each blog and get the most recent post
    // published by $user_id
    foreach ((array) $user_blogs as $blog) {
        $prefix = $wpdb->get_blog_prefix($blog->userblog_id);
        $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);
        // Make sure we found a post
        if (isset($recent_post['ID'])) {
            $post_gmt_ts = strtotime($recent_post['post_date_gmt']);
            // If this is the first post checked or if this post is
            // newer than the current recent post, make it the new
            // most recent post.
            if (!isset($most_recent_post['post_gmt_ts']) || $post_gmt_ts > $most_recent_post['post_gmt_ts']) {
                $most_recent_post = array('blog_id' => $blog->userblog_id, 'post_id' => $recent_post['ID'], 'post_date_gmt' => $recent_post['post_date_gmt'], 'post_gmt_ts' => $post_gmt_ts);
            }
        }
    }
    return $most_recent_post;
}