Create a page that automatically redirects to latest post of a specific tag/category?

I’m looking to make a URL like for example:

http://localhost/latest-news

Where the page for latest-news will automatically redirect to the latest news article (article with news category tag).

Read More

I then will need to use the link /latest-news in a few different places (menu, and a slider on the front page).

Related posts

Leave a Reply

1 comment

  1. I’m going to list the steps below which worked for me:

    Step 1) In the Admin Panel create a Page called “Latest News” which will have the slug you want latest-news.

    Step 2) In your Theme Folder create a blank file and save it as page-latest-news.php. Inside the file, add this:

    <?php
        $recent = wp_get_recent_posts(array('numberposts' => 1));
        $blogID = get_option('page_for_posts');
    
        if(!empty($recent))
            wp_redirect(get_permalink($recent[0]->ID) ,307);
        else
            wp_redirect(get_permalink($blogID), 307);
    

    Step 3) Upload your page-latest-news.php file and test it out.

    I decided to go with 307 redirects because of the Temporary status, which I imagine the latest post will change frequently. Hope it works for you!