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

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;
share|improve this question
up vote 1 down vote accepted
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.

share|improve this answer
So how does this deal with custom posts? And what if the query string has criteria affecting which posts should be available to the loop? – dnagirl Oct 19 '11 at 16:43

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.

share|improve this answer

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


function flush_rewrite_rules()
    global $pagenow, $wp_rewrite;

    if ( 'themes.php' == $pagenow && isset( $_GET['activated'] ) )

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');

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');
share|improve this answer
Don't flush rewrite rules on every init. They should be flushes once, either on plugin or theme activation. – chrisguitarguy Oct 20 '11 at 16:41
Double that. No flush_rewrite_rules. This core stuff meant to get triggered via UI button. – kaiser Oct 21 '11 at 2:43
With this approach you will loose information about the query filter because you override the parameters initially provided by the nice url. Any url ending by /last would only redirect you to the custom template without knowing where you come from and where you should go... You'll then have to do even more work retrieving the bits of the url to rebuild the corresponding query. I think that extending the existing rewrite rules is a better solution. Don't hesitate to show me the way if I miss something here ;-) – Jonathan Liuti Oct 21 '11 at 13:32
I'm not sure about the problem you're seeing here, the example shows how to capture custom url params and then change the default behavior. Isn't a production ready code, but with some tweaks on "add_rewrite_method" you can add as many filters as you need. – Alex Sancho Oct 21 '11 at 14:49
I just think it's more efficient to change the value of the query var posts_per_page and to keep the use of the "default" template and just play around with the rewrites. With your technique you will have to do a lot of templating for nothing IMO. But I'm probably scratching a hitch here ;-) – Jonathan Liuti Oct 21 '11 at 15:48

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.