lewismcarey
2/10/2014 - 4:23 PM

WORDPRESS Oldest Post Date

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' );