Function and usage of get_most_recent_post_of_user() in wordpress


Answers ( 1 )


    The get_most_recent_post_of_user() function in WordPress is used to retrieve the most recent post made by a specific user across all the blogs they have access to. This function is particularly useful in a multi-site environment where a user may contribute to multiple blogs. Here's a detailed explanation of the function and its usage:

    Function Signature:

    get_most_recent_post_of_user( int $user_id ): array


    • Gets a user's most recent post.


    • The function iterates through each blog that the user has contributed to.
    • It checks the post_date_gmt field of the posts to determine the most recent one.
    • The post_date_gmt refers to the date and time the post was made in GMT (Greenwich Mean Time).


    • $user_id (int, required): The unique identifier of the user. You need to pass the user's ID to this function to search for their posts.

    Return Value:

    • Returns an array that contains the following components:
      • blog_id: The ID of the blog where the most recent post was made.
      • post_id: The ID of the post itself.
      • post_date_gmt: The date and time the post was published, in GMT.
      • post_gmt_ts: A timestamp representation of post_date_gmt.

    Sample Usage:

    $user_id = 1; // Replace with the actual user ID
    $recent_post = get_most_recent_post_of_user($user_id);
    if (!empty($recent_post)) {
        echo 'Blog ID: ' . $recent_post['blog_id'];
        echo 'Post ID: ' . $recent_post['post_id'];
        echo 'Post Date (GMT): ' . $recent_post['post_date_gmt'];
        // Other processing...
    } else {
        echo 'No posts found for this user.';

    In this sample, $user_id is the ID of the user whose most recent post you want to find. The function get_most_recent_post_of_user() is called with this user ID, and it returns an array with details about the most recent post. You can then access the blog_id, post_id, and post_date_gmt from this array for further use. If the user has not made any posts, the returned array will be empty.

Leave an answer