0

Been pulling my hair out trying to get related articles going. I've tried a few methods but this is the only one that has given me results but random posts are shown rather than posts related to it by article.

<?php           
    $args=array('tag_in'=>$tags,
    'exclude'=>$post->ID,
    'post_per_page'=> 4,
    'ignore_sticky_posts'=>1,
    'post_type'=>array('ms','gnd','events','news_article','opinions','projects','tenders','videos','products'));
    $rel_pst=get_posts($args);
    $count = 0;
    if($tags){
        foreach($rel_pst as $rel):setup_postdata($rel);//Loop through and find related posts
            if($count==4)
            {
                break;
            }
        $image = wp_get_attachment_image(get_post_thumbnail_id($rel->ID),'related-posts');
        //$tagy=$tags[$count];
        //Counts iterations to place aricles on seperate sides 


        if (($count  == 0)||($count == 2)){// first article start
            echo '<div class="posts_wrapper">';
            echo '<article class="item_left">';
        }
        if(($count == 1)||($count == 3)){// second article start
            echo'<article class="item_right">';
        }
?>  
<div class="pic"> <a href="<?php echo get_permalink($rel); ?>" class="w_hover img-link img-wrap"> <?php echo $image; ?></a> </div>
<h3><a href="<?php echo get_permalink($rel);?>"><?php echo get_the_title($rel); ?></a></h3>
<div class="post-info"> <a href="<?php echo get_permalink($rel);?>" class="post_date"></a> <a href="<?php echo get_permalink($rel); ?>" class="comments_count"></a> </div>
</article>
<?php 
    if(($count == 1)||($count == 3)){//second article/4th end
        echo '</div>';
    }   
        $count ++;
        endforeach;
    }           
//wp_reset_postdata(); ?>

Any help will be appreciated

0

1) It's tag__in (two underscores) 2) get_the_tags() returns an array of objects, you need IDs:

if ( $the_tags = get_the_tags() ) {
    // https://developer.wordpress.org/reference/functions/wp_list_pluck/
    $the_tags = wp_list_pluck( $the_tags, 'term_id' );
}

$rel_pst = get_posts( array(
    'tag__in'             => $the_tags,
    'exclude'             => $post->ID,
    'post_per_page'       => 4,
    'ignore_sticky_posts' => true,
    'post_type'           => array(
        'ms',
        'gnd',
        'events',
        'news_article',
        'opinions',
        'projects',
        'tenders',
        'videos',
        'products',
    ),
));

if ( $rel_pst ) {
    // Do your thing
}
  • I will except this as an answer as it works!! Thanks DeadMedic, I owe you one :) – Zayd Bhyat May 5 '16 at 9:40
1

Whithout knowing the value of $tags variable (you are not showing it to us), the only thing I see wrong in your code is the tag_in argumente, the correct one is tag__in (note the double _).

Also, note that tag__in works with the core tag taxonomy. This taxonomy is not supported by custom post types by default, only by the standard post type.

  • Ah interesting, so do you have any suggestions for custom post types? Or how I can go about comparing tags of a post against all post types with the same tag(s). Also changing the query to tag__in breaks the query and no results are displayed – Zayd Bhyat May 5 '16 at 8:24
  • Oh and $tags is $tags=get_the_tags(); From the original post – Zayd Bhyat May 5 '16 at 8:25
  • 1
    +1 For opening the discussion for $tags and highlighting the tag_in typo. – TheDeadMedic May 5 '16 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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