最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp query - Wordpress Related Post by tags in Single.php

programmeradmin4浏览0评论

I want to create a related post, but the code I tried shows only related post using only the first tag. I want related post which use all tags. (Because my site is a blog and each post can have up to (max)5 tags)

I want to show related post, based on the first tag and then also using the other (max 5)tags.

The code shown isn't working as wished, what is wrong?

$tags = wp_get_post_tags($post->ID);
if ($tags) {
    echo 'Related Posts';
    $first_tag = $tags[0]->term_id;
    $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'posts_per_page'=>5,
        'caller_get_posts'=>1
        );
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><?php

    endwhile;
    }

wp_reset_query();
}

I want to create a related post, but the code I tried shows only related post using only the first tag. I want related post which use all tags. (Because my site is a blog and each post can have up to (max)5 tags)

I want to show related post, based on the first tag and then also using the other (max 5)tags.

The code shown isn't working as wished, what is wrong?

$tags = wp_get_post_tags($post->ID);
if ($tags) {
    echo 'Related Posts';
    $first_tag = $tags[0]->term_id;
    $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'posts_per_page'=>5,
        'caller_get_posts'=>1
        );
    $my_query = new WP_Query($args);

    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><?php

    endwhile;
    }

wp_reset_query();
}
Share Improve this question edited Jan 16, 2020 at 22:29 Charles 2,28718 silver badges24 bronze badges asked Jan 16, 2020 at 18:11 Teejah JamesTeejah James 174 bronze badges 4
  • You could try those solutions : stackoverflow/questions/29395419/… – Patrice Poliquin Commented Jan 16, 2020 at 19:43
  • Related: wordpress.stackexchange/questions/98024/… – Jesse Nickles Commented Jan 27, 2024 at 9:28
  • Related: wordpress.stackexchange/questions/183494/… – Jesse Nickles Commented Jan 27, 2024 at 9:42
  • Does this answer your question? Related Posts by Multiple Tags? – Jesse Nickles Commented Jan 27, 2024 at 9:48
Add a comment  | 

1 Answer 1

Reset to default 1

The code shown isn't working as wished, what is wrong?

you're using only the first tag : $first_tag = $tags[0]->term_id; to grab posts from all tags you have to iterate the whole array until a max of 5. The code below ( untested) grabs a max of 25 posts theoretically using up to 5 tags..BUT if you have 25 posts belonging to the first tag you will have only posts related to the first tag anyway:

$tags = wp_get_post_tags($post->ID);
if ($tags) {
  echo 'Related Posts';
  $max=5;
  $arrayTags=[];
  for($k=0;$k<$max;$k++){
    $arrayTags[]=$tags[0]->term_id;
  }

  $args=array(
    'tag__in' => $arrayTags,
    'post__not_in' => array($post->ID),
    'posts_per_page'=>25,
    'ignore_sticky_posts'=>1
  );
  $my_query = new WP_Query($args);

  if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
 <?php
  endwhile;
  }
wp_reset_query();
}

to be sure to grab 5 posts from 5 tags you should run 5 queries ( unless there's a more efficient way to do that of which I'm not aware of )

$tags = wp_get_post_tags($post->ID);
if ($tags) {
    echo 'Related Posts';
    $max=count($tags) >5 ? 5 : count($tags) ;
    $idsToExclude=[$post->ID];
    //ob_start();
    for($k=0;$k< $max ;$k++){
        $args=array(
        'tag__in' => $tags[$k]->term_id,
        'post__not_in' => $idsToExclude,
        'posts_per_page'=>5,
        'ignore_sticky_posts'=>1 // caller_get_posts is deprecated
        );
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
            while ($my_query->have_posts()) : $my_query->the_post(); 
            if(!in_array(get_the_id(),$idsToExclude)){
                $idsToExclude[]=get_the_id();   // add this id to the exclusion for the next query
            }           
            ?>
            <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a><br>
            <?php
            endwhile;
        }

    }
    //echo ob_get_clean();
    wp_reset_query();
}
发布评论

评论列表(0)

  1. 暂无评论