url rewriting for most recent post

I’d like to set up my WP so that urls ending in /last would return the most recent post in that set of posts. For example, /last returns the most recent post of any type, archives/category/trivia/last would return the most recent post categorized as trivia, and archives/LoomSong/last would return the most recent custom post of type=’LoomSong’.

I think I need to add rewrite rules, but I’m very unsure of what the regex should be. So far this is what I have:

add_action( 'init', 'ASHmostrecent_init' );
function ASHmostrecent_init(){
    add_rewrite_rule( 'last/?$', 'WHAT DO I PUT HERE?');
}

add_filter( 'query_vars', 'ASHmostrecent_query_vars' );
function ASHmostrecent_query_vars( $query_vars ){
    $query_vars[] = 'ASHmostrecent_filter';
    return $query_vars;
}

Related posts

Leave a Reply

3 comments

  1. add_action( 'init', 'ASHmostrecent_init' );
    function ASHmostrecent_init(){
        add_rewrite_rule( 'last/?$', 'index.php?post_type=post&posts_per_page=1');
    }
    

    A query already pulls up posts starting from the latest to the oldest, so all you have to do is limit what’s returned by one. posts_per_page does this; limiting by one will give you the latest post. Edit the post_type accordingly.

  2. WordPress’s default behavior is already to return latest items sorted by date in descending order.
    I don’t quite understand the point of having this “/last” parameter.
    Maybe are you just trying to limit the number of results as Manny Fleurmond suggested?

    If it’s the case we have 2 different problems to solve here:

    1. Dealing with the display of every post types on the /last url alone.
    2. Having only a set of latest item displayed when the /last extension is used.

    If you go the rewrite way, you’ll have to rewrite every single rule of wordpress that you want to support and there is quite a lot of them. And then add filters to actually change the behavior of the query depending on the url and the parameter retrieved.

    You will also have to deal with the possibility to have a post/page or category named “last”.

    This is not something easy to do and it requires a lot of work, I can’t decently provide a working example because of the time it would involve.

    some ref that could help:
    post_limits filter,
    Justin Tadlock article

    Good luck.

  3. You can try this approach, the idea is to use a custom template inside your theme that handles custom queries based on url keyword

    <?php
    
    function flush_rewrite_rules()
    {
        global $pagenow, $wp_rewrite;
    
        if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )
            $wp_rewrite->flush_rules();
    }
    
    function add_rewrite_rules($wp_rewrite)
    {
        $new_rules = array('(.+)/last' => 'index.php?last=true';
        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
    }
    
    function add_query_vars($qvars)
    {
        $qvars[] = 'last';
        return $qvars;
    }
    
    function template_redirect_file()
    {
        global $wp_query;
    
        if ($wp_query->get('last'))
        {
            if (file_exists(TEMPLATEPATH . '/last.php'))
            {
                include(TEMPLATEPATH . '/last.php');
                exit;
            }
        }
    }
    
    add_action('load-themes.php', 'flush_rewrite_rules' );
    add_action('generate_rewrite_rules', 'add_rewrite_rules');
    add_filter('query_vars', 'add_query_vars');
    add_action('template_redirect', 'template_redirect_file');