I created a custom post type named predic
, Iâm trying to get within one of this post other four random posts from the same custom post type. I used this code, but I keep getting the same post Iâm in 4 times.
<ul>
<?php
$rand_posts = get_posts('numberposts=4&orderby=rand');
foreach( $rand_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
You need to include
setup_postdata($post);
in your foreach line. Here’s some great demo code from the codex, adopted to fit your query:Note that we’re also resetting $post back to the current post, so we don’t break other functionality.