WordPress Tip: Get the date of your first post

I’m just working on setting up a custom WordPress archive for a client at the moment (displaying posts by academic year).

In order to do things absolutely right (and in case they decide to post-publish older items), I need to be able to check when the first post was published on their WordPress installation.

I figured I’d share the code out here in case someone else finds it useful (and in case anyone can suggest improvements to the code). I’ve wrapped it up as a function so you can throw it your functions.php if it’s something you’ll re-use.

 * Get First Post Date Function
 * @param  $format Type of date format to return, using PHP date standard, default Y-m-d
 * @return Date of first post
function ax_first_post_date($format = "Y-m-d") {
 // Setup get_posts arguments
 $ax_args = array(
 'numberposts' => -1,
 'post_status' => 'publish',
 'order' => 'ASC'

 // Get all posts in order of first to last
 $ax_get_all = get_posts($ax_args);

 // Extract first post from array
 $ax_first_post = $ax_get_all[0];

 // Assign first post date to var
 $ax_first_post_date = $ax_first_post->post_date;

 // return date in required format
 $output = date($format, strtotime($ax_first_post_date));

 return $output;

To echo it anywhere on your site just run:

echo ax_first_post_date();

I’m hoping to be a little better at keeping track of useful WordPress functions that could be reused, so have created a sub-category of my WordPress category called “Functions”. If nothing else it will be a useful reference point for me.


As soon as I hit publish I knew I should have held off until I added in a date format parameter. I’ve updated the code now so that you can tell the function what format the date should be returned in (using standard PHP date format).


  • kamiel

    isn’t get_posts() too expensive for this? I’d suggest using a custom Mysql query using SELECT MIN(date), could be a lot faster?
    Like so: SELECT min(post_date) FROM wp_posts WHERE post_status = ‘publish’ AND post_type = ‘post’

    • That’s probably a very fair point.

      I wonder could you stay in the WordPress functions just by changing the args to

      ‘numberposts’ => 1,
      ‘post_status’ => ‘publish’,
      ‘orderby’ => ‘date’
      ‘order’ => ‘ASC’

      No need to get all posts at all, just get one post but order by oldest first.

      • Ajay

        I used 'numberposts' => 1 to achieve this. It’s a lot faster and lighter than getting all the posts

    • Lewis M Carey

      You could maybe add this to a function in functions.php

      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'”,

      $result = strtotime($wpdb->get_var( $query ));

      $output = date($format, $result);

      return $output;


      • Almost certainly running a direct query would be faster than involving WPs somewhat hefty query structure.

        • Lewis M Carey

          Yeah and I thought passing the post type allows you to utilise this on custom post types too:

          ## Usage

          echo oldest_post_date( ‘post’ );
          echo oldest_post_date( ‘your_custom_post_type’, ‘d-m-Y’ );

  • Hey Alex,

    Thanks a lot for sharing this snippet. I just completed my first plugin(http://wpblogexperts.com/plugins/auto-footer-copyright/) and this snippet came in very helpful. Would have taken ages for me to figure it out.

    Thanks again. 🙂

    • Hey Ishan,

      Delighted to hear it was useful!

      Good idea on the plugin – I’ve been meaning to do a few simple plugins like that but never get around to it!


      • Thanks Alex.

        I will now try to make it universal. It is limited to Genesis Framework for now 🙁 These are small annoyances that are solved in 2-3 minutes (I’ve removed default stuff and added custom footers like 30-40 times till now). Hope I see your ideas pluginised soon. 🙂

  • DanielLemes

    Pretty helpful, thanks. Hard to believe WordPress doesn’t have an easier way to do this.