I have this PHP code that shows a related posts element created using advanced custom fields plugin. I want to create a shortcode inside functions.php with the code and then use the shortcode in a text element of a page builder. Would someone kindly assist me with the modified code to put inside functions.php? Thanks
<?php
$posts = get_field('related_posts', false, false);
$loop = new WP_Query(array('post_type' => 'post', 'posts_per_page' => 3, 'post__in' => $posts, 'post_status' => 'publish', 'orderby' => 'post__in', 'order' => 'ASC' ));
if($loop->have_posts()) { ?>
<div class="rel-posts">
<?php while ($loop->have_posts()) : $loop->the_post(); ?>
<div class="related-post">
<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('td_218x150'); ?></a>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php endwhile; ?>
</div>
<?php } wp_reset_query(); ?>