How can I link to the most recent post in a category?

How can I create a link to a category–to it’s most current/recent post?

I want to display the actual post. I do not want to display the category with one post listing in it.I’m going to be really silly here… 🙂

Read More

But in my sometimes wild and limitless imagination something like this should already be available:

domain.com/category-name/index.php?newest

or

domain.com/category/category-name/index.php?latest&wpusers=happier-now

I’ve searched and there are many PHP level solutions. And this site won’t let me post more than two links here so sorry about the limitation feature

…and for reference here are the best of the related links I looked into (some are 100% in context but dated so I don’t know if they’re still effective with WordPress):

Resolved

Link to most recent post in one category?

Link to latest post in category

I had about ten more relevant links…

But again, it really seems like there doesn’t need to be a PHP, function or template level way ONLY to do this. There are so many cool little ?this and &that elements added to the URL that there should be one that gets the latest post.

A good example is a YouTube playlist link. It loads the latest video and allows all kinds of &height &width &rel=0 abilities.

I’m kinda surprised this isn’t a core WordPress feature.

Right now I’ve got several widget variations which can display a recent or featured post on the front page with an excerpt and a featured image. When someone clicks on the title, featured image, or Read More, they’re taken to that post.

So I can see widgets can create the links–I’m just out of my depth figuring out how to leverage the same concept to create a link without displaying everything.

Related posts

1 comment

  1. It’s not built into core, but it’s certainly possible to add, however – I wouldn’t consider it a good idea to have a single post available at multiple URLs, so a redirect is probably best. Of course, it will require a bit of PHP, as WordPress doesn’t operate on magic or willpower.

    First, we hook a function to the parse_request action, which runs when WordPress is determining how to set the query vars for the main query.

    The following bit of code assumes pretty permalinks are enabled, and category URLs have a category base. Under these conditions, the category_name query var is set, so we can check if this is a request for a category. At the same time, we also check if a latest GET var is set, so together this code will be triggered when a URL looks like:

    http://example.com/category/some-category/?latest

    If those conditions are met, we query for a single post in the requested category name, via WP_Query, which by default will give us the latest post in that category.

    If a post is found, we redirect to that post’s URL via wp_redirect.

    This bit of code can go in our theme’s functions.php file:

    function wpa_latest_in_category_redirect( $request ){
        if( isset( $_GET['latest'] )
            && isset( $request->query_vars['category_name'] ) ){
    
            $latest = new WP_Query( array(
                'category_name' => $request->query_vars['category_name'],
                'posts_per_page' => 1
            ) );
            if( $latest->have_posts() ){
                wp_redirect( get_permalink( $latest->post->ID ) );
                exit;
            }
    
        }
    }
    add_action( 'parse_request', 'wpa_latest_in_category_redirect' );
    

Comments are closed.