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="nofollow noreferrer noopener" 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!
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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_queryto 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 atax_query -
Use
randas value to theorderbyparameter inWP_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="nofollow noreferrer noopener" rel="bookmark" title="<?php the_title(); ?>" rel="nofollow"><?php the_title(); ?></a></h5>
<?php the_excerpt(); ?>
</div><!--ncc-->
<?php }
wp_reset_postdata();
echo '</div><!--related-->';
}
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0