最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can get the last post date of the user?

programmeradmin2浏览0评论

I want to get the last post date was written by the user and here's the code that I have made but it's not working:

function get_user_last_post_date( $user_id ) {

    $args = array(
        'post_author' => $user_id,
        'post_type' => 'any',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'order' => 'DESC',
        'orderby ' => 'post_date'
    );
    $latest_posts = new WP_Query( $args );

    $last_date = '';
    if ( $latest_posts->have_posts() ) {
        $last_date = $latest_posts;
    }

    return $last_date;
}

I want to get the last post date was written by the user and here's the code that I have made but it's not working:

function get_user_last_post_date( $user_id ) {

    $args = array(
        'post_author' => $user_id,
        'post_type' => 'any',
        'post_status' => 'publish',
        'posts_per_page' => 1,
        'order' => 'DESC',
        'orderby ' => 'post_date'
    );
    $latest_posts = new WP_Query( $args );

    $last_date = '';
    if ( $latest_posts->have_posts() ) {
        $last_date = $latest_posts;
    }

    return $last_date;
}
Share Improve this question asked Dec 26, 2019 at 13:52 JohnJohn 1133 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

In your sample code the $latest_posts is an instance of WP_Query class. If you want to get the latest post's date, you should do this:

$latest_posts = new WP_Query( $args );

$last_date = '';
if ( $latest_posts->have_posts() ) {
   $latest_posts->the_post();
   $last_date = get_the_date();
}

return $last_date;
发布评论

评论列表(0)

  1. 暂无评论