How to Get the Link to the Latest Post in WordPress

How to Get the Link to the Latest Post in WordPress

As you write more and more blog posts, the challenge is to make use of your extensive content and convert readers who might be viewing your older posts, to continue staying on the site and check out your latest content. You probably have a few “evergreen” articles on your blog which might be old, but continue to generate a lot of interest. Wouldn’t it be nice if readers of these posts got an opportunity to see what’s new on your blog?

To solve this, we’ve looked at various solutions in the past. We can have a widget of recent posts on the sidebar for example, or a tableau of “related content” that draws from potentially relevant articles that you’ve written previously. However, sometimes all you want is a simple link to the latest blog post on your website towards the end of the article.

In this post, I’ll demonstrate an easy function to get an HTML link to the latest WordPress blog post including the title. No need for plugins or additional files – just a snippet of code added to functions.php!

Getting the Link to the Latest WordPress Post

Open up your functions.php file and add the following code towards the end before the closing ?> PHP tag:

function get_latest_post_link(){
    global $post;
    $placeholder = $post;
    $args = array(
        'numberposts'     => 1,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_status'     => 'publish' );
    $sorted_posts = get_posts( $args );
    $permalink = get_permalink($sorted_posts[0]->ID);
    $title = $sorted_posts[0]->post_title;
    $post = $placeholder;
    $latest_link_html = 'Latest Link: <a href="'.$permalink.'">'.$title.'</a>';
    return $latest_link_html;
}

If you don’t know how to add custom code to WordPress, check out my earlier tutorial on the same. The code above doesn’t do anything by itself. It simply creates a function that returns the HTML output containing the link to the latest post, with the title. As an example, here’s a screenshot of the latest post on my test blog called “Demo Post”:

latest postou

Now let’s use this code to insert a link towards the end of every blog post on the site. For this,  we’re going to use the “the_content” filter and append the link. Past the following code after the one we’ve just inserted:

function link_to_most_recent_post($content) {
    $latest_link = get_latest_post_link();
   return $content .= $latest_link;
}
add_filter( 'the_content', 'link_to_most_recent_post');

In this, we use the first function to get the HTML of the link to the latest post, and then simply append it to the existing content. In the screenshot below, you can see how it looks at the end of a random post.

link to latest post added

We can modify this function in many ways. For example, it looks a bit clumsy to have the latest post link to itself! So we can insert a condition in the original function that checks to see if the permalink we generated is the same as the permalink of the current post, and if so, return nothing. That looks like this:

function get_latest_post_link(){
    global $post;
    $current_permalink = get_permalink();
    $placeholder = $post;
    $args = array(
        'numberposts'     => 1,
        'offset'          => 0,
        'orderby'         => 'post_date',
        'order'           => 'DESC',
        'post_status'     => 'publish' );
    $sorted_posts = get_posts( $args );
    $permalink = get_permalink($sorted_posts[0]->ID);
    if ($permalink == $current_permalink)
        return;
    $title = $sorted_posts[0]->post_title;
    $post = $placeholder;
    $latest_link_html = 'Latest Link: <a href="'.$permalink.'">'.$title.'</a>';
    return $latest_link_html;
}

The code in bold indicates the new additions where we first obtain the current permalink and compare it with the generated one. If they match, we simply go back.

We can even implement tracking by adding a custom query parameter like ?referrer=latest to the  end of the URL so that we can measure the effectiveness of our strategy. In fact, this is a great idea to track your experiments in general. To learn more about how to do this, take a look at my earlier tutorial on tracking internal links in WordPress.

By adding a link to the latest post, you have a chance at converting random readers who stumble upon an old article into loyal blog readers – or at the very least, keep them on your site for a bit longer!

2 Comments on “How to Get the Link to the Latest Post in WordPress”!

  1. Avatar

    Hi Bhagwad.

    Great article and simple solution. I am trying to incorporate your method into my site where it displays recent posts where it has three button links, an image box that displays the post featured image when hovering over one of the buttons. Would this be to complex for this solution?

    Any help would be greatly appreciated.

    Regards,

    Roudy

    • Bhagwad Park

      Hi Roudy! Yeah, I think what you’re looking for would be better accomplished with a plugin. That way, you can customize everything just the way you want it.

Leave a Reply

This Website is Hosted by

Powered by RamNode

Disclosure: We receive a compensation from some of the companies whose products or services are presented on our website.