Skip to content

Instantly share code, notes, and snippets.

@jacobwise
Last active August 29, 2015 14:14
Show Gist options
  • Save jacobwise/81f568ba8ceeaf47caf3 to your computer and use it in GitHub Desktop.
Save jacobwise/81f568ba8ceeaf47caf3 to your computer and use it in GitHub Desktop.
Using c.bavota and from the WordPress Codex for human_time_diff() these function return a readable time since last post for custom post types
/**
* Display time since post was published
*
* @uses human_time_diff() Return time difference in easy to read format
* @uses get_the_time() Get the time the post was published
* @uses current_time() Get the current time
*
* @return string Timestamp since post was published
*
* @author c.bavota
*/
function get_time_since_posted( ) {
$time_since_posted = human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago';
return $time_since_posted;
}
function jw_time_since_last_post($post_type)
{
$args = array(
'post_type' => $post_type,
'posts_per_page' => 1,
'no_found_rows' => true,
'fields' => 'ids'
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
return get_time_since_posted( );
}
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment