Link each category to last post

You can filter category_link and replace the URL here. I have used the newest post in the following example, because first could also mean the oldest and that sounds … strange. 🙂

add_filter( 'category_link', 'wpse_96677_cat_link_to_first_post', 10, 2 );

function wpse_96677_cat_link_to_first_post( $url, $term_id )
{
    $term = get_term( $term_id, 'category' );

    // sub-terms only
    if ( ! isset ( $term->parent ) or 0 == $term->parent )
        return $url;

    $post = get_posts(
        array(
            'numberposts' => 1,
            'category'   => $term_id
        )
    );

    if ( ! $post )
        return $url;

    return get_permalink( $post[0]->ID );
}

Leave a Comment