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.

About Craig Lotter

Software developer, husband and dad to two young ladies. Writer behind An Exploring South African. I don't have time for myself any more.