admin管理员组

文章数量:1125364

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!

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!

Share Improve this question edited Feb 2, 2024 at 20:06 Jesse Nickles 7357 silver badges19 bronze badges asked Apr 7, 2015 at 18:16 JornesJornes 7535 gold badges12 silver badges31 bronze badges 3
  • I just need to know how to exclude one tag – Moreentje Commented Mar 31, 2022 at 9:32
  • Related: wordpress.stackexchange.com/questions/98024/… – Jesse Nickles Commented Jan 27, 2024 at 9:41
  • Related: stackoverflow.com/questions/29395419/… – Jesse Nickles Commented Jan 27, 2024 at 10:57
Add a comment  | 

1 Answer 1

Reset to default 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-->';
}
?> 

本文标签: Display related posts randomly that match quotanyquot existing tags