How do I simply retrieve the last most recent publish post in wordpress with mysql?

I’m looking to retrieve the most recently published post in a wordpress system. I have some basic mysql understanding and I can find where the revisions are stored in the wp_posts table but what I can’t seem to find is how to retrieve the permalink for the most recent post.

any ideas on how to retrieve the most recent post with mysql and permalink?

Read More

I see there are some existing functions from WP like so:

// get the latest blog entry
$myposts = get_posts('numberposts=1');
foreach($myposts as $post) :

    echo '<a href="' . the_permalink() . '">' . the_title() . '</a>';
endforeach;

But when I put this on a custom page I’m working on, it seems to just be pulling out the page name I’m currently on and the link to this page (even though I’m thinking the above function should be retrieving a ‘post’.

What am I missing?

Related posts

Leave a Reply

2 comments

  1. Just in case you may want to have a real MySQL solution, here’s what I use:

    $query = "SELECT * FROM wp_posts WHERE post_type='post' AND post_status='publish' ORDER BY post_date DESC LIMIT 1";
    $post = mysql_fetch_assoc(mysql_query($query));
    

    now the $post array holds all the data regarding to the latest post.

  2. Worked out the solution:

    <?php
    
    global $post; // needed this
    
    // get the latest blog entry
    $myposts = get_posts('numberposts=1&orderby=date&order=DESC'); // and more stuff here
    
    foreach($myposts as $post) :
    
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
    <?php endforeach; ?>