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;
}
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 thepost_type
accordingly.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:
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.
You can try this approach, the idea is to use a custom template inside your theme that handles custom queries based on url keyword