Last active
August 29, 2015 13:56
-
-
Save lewismcarey/8918952 to your computer and use it in GitHub Desktop.
WORDPRESS Oldest Post Date
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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