WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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 would like to load the last post when I go to the selected category.

For example
if I go to http://site.com/category
it loads http://site.com/category/35/this-is-the-last-post

Does exist some example to follow about that? I'm doing practice with Wordpress and I don't know how can I get the last post of a category.

share|improve this question

Hook template_redirect and check if it's a category archive page, if so get the category ID from query_vars and query_posts for a single post from that category, which by default should be the most recent, then wp_redirect to the post's permalink.

This code would go in your theme's functions.php file:

function my_category_redirect() {
    if ( is_category() ) :
        query_posts('cat='. get_query_var('cat') .'&posts_per_page=1');
        wp_redirect( get_permalink() , 301 ); 
add_action( 'template_redirect', 'my_category_redirect' );
share|improve this answer

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.