Improved code – Show related posts in WordPress without a plugin

A while ago I posted an article about showing related posts in WordPress without the use of a plugin. Recently I have found better code for this and I would like to share it with you

<?php
$tag_ids = wp_get_post_tags( $post->ID, array( 'fields' => 'ids' ) );
$args = array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=> 5,
'no_found_rows' => true,
'update_post_meta_cache' => false,
'update_post_term_cache' => false
);
$tag_query = new WP_Query($args);
if( $tag_query->have_posts() ) : ?>
<h3 class="widget-title">You might also like:</h3>
<ul>
<?php while ($tag_query->have_posts()) : $tag_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Open <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
<?php endif; ?>

The difference with the previous code is that this code only retrieves the ID’s from the related tags, where the previous code retrieved all the information from the tags, to use only the ID from the array.

The code is cleaner and does not use any if anymore