WordPress PHP: Get the Month and Year of the Newest Post on Your Blog CodeUnit 02 JAN 2012

To retrieve the month and day of the newest 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 newest post in the database and then extract the relevant information from there.

The code is as follows:

//Default values for the resulting variables
$newestyear = date('Y');
$newestmonth = date('m');
//Calculate newest Post Date
$args = array('orderby'=>'date','order'=>'DESC','posts_per_page'=>1,'caller_get_posts'=>1);
$newestpost = get_posts($args);

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

//Print out the result            
echo "

The newest post of this blog appeared in the $newestmonth month of $newestyear


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.