0

What's the best way to get the permalink to the latest post of a chosen category? I have a page that I want to have several links to the latest post from different specified categories.

Simple question, but I'm not sure if it's even possible :)

Thanks!

1

This will work:

$latest_post = get_post( array( 'cat' => YOUR_CAT_ID, 'posts_per_page' => 1) )
if( $latest_post ) {
    echo get_permalink( $latest_post->ID );
}

Or you can do it in bulk for more categories:

$categories = array( 7, 12, 14, 15 );

foreach( $categories as $cat ) {
    $latest_post = get_post( array( 'cat' => $cat, 'posts_per_page' => 1) )
    if( $latest_post ) {
        echo get_permalink( $latest_post->ID );
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.