WordPress PHP: Get the Month and Year of the Oldest Post on Your Blog CodeUnit 23 DEC 2011

To retrieve the month and day of the oldest post on your blog via PHP (which you could of course include in a function) is relatively simple – in essence all that you are doing is putting in a call for the WordPress engine to return to you the oldest post in the database and then extract the relevant information from there.

The code is as follows:

//Default values for the resulting variables
$oldestyear = date('Y');
$oldestmonth = date('m');
//Calculate Oldest Post Date
$args = array('orderby'=>'date','order'=>'ASC','posts_per_page'=>1,'caller_get_posts'=>1);
$oldestpost = get_posts($args);

//Extract the date information            
if (!empty($oldestpost)){
    $oldestyear = mysql2date('Y',$oldestpost[0]->post_date);
    $oldestmonth = mysql2date('m',$oldestpost[0]->post_date);

//Print out the result            
echo "

The oldest post of this blog appeared in the $oldestmonth month of $oldestyear


As simple as that.

Related Posts:

About Craig Lotter

South African software architect and developer at Touchwork. Husband to a cupcake baker and father to two little girls. I don't have time for myself any more.