Skip to content

Instantly share code, notes, and snippets.

@lewismcarey
Last active August 29, 2015 13:56
Show Gist options
  • Save lewismcarey/8918952 to your computer and use it in GitHub Desktop.
Save lewismcarey/8918952 to your computer and use it in GitHub Desktop.
WORDPRESS Oldest Post Date
/**
* Oldest Post Date
*
* @author lewis
*
*/
function oldest_post_date( $post_type, $format = "Y-m-d" )
{
global $wpdb;
$query = $wpdb->prepare(
"SELECT min(post_date) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type = '%s'",
$post_type
); // pass through the post type
$result = strtotime($wpdb->get_var( $query )); // return the result and format to timestamp
$output = date($format, $result); // format the timestamp
return $output; // return the date
}
# WORDPRESS Oldest Post Date
This function gets the oldest post's published date (status published).
* You must pass it a post type (accepts Custom Post Types)
* You may pass it a date format (http://www.php.net/manual/en/function.date.php)
## Usage
echo oldest_post_date( 'post' );
echo oldest_post_date( 'your_custom_post_type', 'd-m-Y' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment