2

I would like to ask that what code should i add in order to make my related posts by tag to display randomly? I got this code from somewhere else.

<?php $orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>5, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
echo '<div id="related"><h4>Related Posts</h4>';
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
<div class="ncc">
<h5><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>" rel="nofollow"><?php the_title(); ?></a></h5>
<?php the_excerpt(); ?>

</div><!--ncc-->

<? }
echo '</div><!--related-->';
}
}
$post = $orig_post;
wp_reset_query(); ?>

Any solution? Thanks!

3

1 Answer 1

17

Some of the code in OP is a bit old and depreciated, like caller_get_posts which was depreciated years ago. The correct parameter to use now is ignore_sticky_posts. Your query is also really inefficient and not good for performance.

Here is how I would tackle this issue

  • Use get_queried_object_id() to get the current post ID instead of the more unreliable method using the global $post

  • Use wp_get_post_terms() to return all the tag ID's assigned to the post. From what I make, we only need to get ID's, and not the complete tag objects

  • Use a proper tax_query to get all posts that have any of these tags attached to them. This is more personal preference as it is easy to change across taxonomies and also, looking at source code, the tag parameters use a tax_query

  • Use rand as value to the orderby parameter in WP_Query

In short, putting this all in code: (Untested, requires PHP 5.4+)

<?php
$tags = wp_get_post_terms( get_queried_object_id(), 'post_tag', ['fields' => 'ids'] );
$args = [
    'post__not_in'        => array( get_queried_object_id() ),
    'posts_per_page'      => 5,
    'ignore_sticky_posts' => 1,
    'orderby'             => 'rand',
    'tax_query' => [
        [
            'taxonomy' => 'post_tag',
            'terms'    => $tags
        ]
    ]
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
    echo '<div id="related"><h4>Related Posts</h4>';
        while( $my_query->have_posts() ) {
            $my_query->the_post(); ?>
            <div class="ncc">

                <h5><a href="<?php the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h5>
                <?php the_excerpt(); ?>

            </div><!--ncc-->
        <?php }
        wp_reset_postdata();
    echo '</div><!--related-->';
}
?> 
5
  • Hi Peter Goosen, amazing! Your code works well! Thank you very much! :)
    – Jornes
    Apr 8, 2015 at 3:12
  • My pleasure, enjoy :-) Apr 8, 2015 at 5:49
  • wow looks great . another question : how we exclude most popular posts from this query ? Post wievs stored in custom field name called (pageView) ? is that possible ? Oct 12, 2016 at 2:03
  • Still works well in PHP 8... it's that Goosen quality ;) Jan 27 at 9:39
  • I can't edit currently, but I'm leaving a note here that the rel="nofollow" snippet should be deleted from this answer. Jan 29 at 7:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.