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
1 Answer
Reset to default 0In 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;