Hooking to walker_nav_menu_start_el to insert list of subpages

I was able to solve my own problem by using get_the_title with the post ID rather than using the_title. Then I decided to try using $subpost->post_title and that worked too. I figure this is better than get_the_title since it doesn’t have to “get” the data again, it just uses the data we’ve already gotten. I have no idea why $subpost->post_title would work and the_title wouldn’t. Would love to hear any insights people have about this.

Working code:

function hw_submenu( $output, $item ) {
   if( $item->object == 'category' ) {

      $hw_output="<ul>";
      $subposts = get_posts( array(
         'posts_per_page' => -1,
         'category' => $item->object_id
      ) );
      if ( $subposts ) {
         foreach ( $subposts as $subpost ) :
            setup_postdata( $subpost );
            $hw_output .= "<li><a href="" . get_the_permalink($subpost->ID) . ""'>" . $subpost->post_title . "</a></li>";
         endforeach; 
         wp_reset_postdata();
      }

      $output .= $hw_output . "</ul>";
   }    

   return $output;
}
add_filter( 'walker_nav_menu_start_el', 'hw_submenu', 10, 2);