0

So i need to get and echo current users last post date.

What i have researched and tested.

Red article here: https://developer.wordpress.org/reference/functions/get_most_recent_post_of_user/

Red this thread here but none of the codes worked: Get how many days since last post of the current user

The best codes i tried was this and it didn't work.

$user_id = wp_get_current_user();
echo get_most_recent_post_of_user( $user_id );

$user_id == 2;
echo get_most_recent_post_of_user( $user_id );
0

Is that the only code you've used? As the name suggests that function returns the entire post, not just the date. You still need to actually get the date from that post.

$user_id = 2;
$post = get_most_recent_post_of_user( $user_id );

if ( $post ) {
    echo get_the_date( 'Y-m-d', $post );
}

See the PHP documentation on date formats for other ways to format the date.

6
  • Your code doesn't work. Gives me a blank page. – ANdy Feb 19 at 15:53
  • The code is fine. Where did you put it? – Jacob Peattie Feb 19 at 15:54
  • header.php and home.php and content.php and index.php tried them all. – ANdy Feb 19 at 15:57
  • Sorry but I'm looking at it real hard and the code is fine. Have you paid attention to where your <?php opening and ?> closing tags are? – Jacob Peattie Feb 19 at 16:05
  • Yes but still it doesn't work. – ANdy Feb 19 at 16:11
0

I found a bad solution but it works for me. If someone wants to improve this code or has a better solution i would be thankfull.

$user_id = get_current_user_id();

        $args=array(
            'post_type' => 'post',
            'post_status' => 'published',
            'posts_per_page' => 1,
            'ignore_sticky_posts' => 1,
            'author' => $user_id
        );                       
        $wp_query = new WP_Query($args);
        while ( have_posts() ) : the_post(); 
        
            echo the_title();   
            echo get_the_time('Y-m-d', $post->ID);
            
        endwhile; 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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