get_posts changes main query

Here is my code in single.php :

<?php if (have_posts()):the_post() ?>
<h3><?php the_title() ?></h3> <!– Prints `Hello World` –>
<?php if (!empty($someOtherPosts = get_posts([‘posts_per_page’ => 3]))): ?>
<ul>
<?php foreach ($someOtherPosts as $post): ?>
<li><?php echo $post->post_title ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<h3><?php the_title() ?></h3> <!– Prints `Bye World` –>
<?php endif; ?>

Why am I getting different title in the next the_title() call and how can I manage this?

Read more here:: get_posts changes main query

Leave a Reply

Your email address will not be published. Required fields are marked *