WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I extract the last published post in Wordpress?

I would be grateful if someone can just point out the relevant action hooks that I should check out. I have seen publish_post, edit_post, save_post, get_the_tags, sanitize_** functions in post.php but so far no success.

I want to export the entire array of the last published post from the wordpress database. How do I do that? thanks.

share|improve this question

This is how I do it. You may want to reset and call it as a function in case you want to reuse.

// Most Recent

function nt_mostrecent( $count ) {

        $my_query = new WP_Query( array(showposts => $count, order => 'DSC', orderby => 'date'));

        while ($my_query->have_posts()) : $my_query->the_post();

            $posts .= '<a href="' . get_permalink() . '">' . get_the_title() . '</a>';


        return $posts;


share|improve this answer
this is a more to-the-point approach... wordpress also has a wp_get_recent_posts($args) function... you can supply it $args = array( 'numberposts' => '1' ); – BAU Mar 24 '12 at 16:13
thanks for your time anyway... :) I dont have enough reputation to give your answer a 1-up for useful.. I will do so as soon as I get 15 points... thanks! – BAU Mar 24 '12 at 16:20
up vote 0 down vote accepted

Thank You! I sorted this out myself thanks...

For any one who stumbles upon this, here's the resolution...

// Get the last n number of posts.

$args = array( 'numberposts' => 'n' ); // replace n with the number of posts
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
    echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.$recent["post_title"].'" >' .   $recent["post_title"].'</a> </li> ';
    echo '<li>Tags '.print_r($recent).'</li> ';
share|improve this answer
How can I use your code to check all the last posts of all custom post types including the native post type post? – Casper Nov 30 '14 at 21:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.