• Hey Guys,

    Im currently working with tags and trying to show post titles and images of different types of posts that have the same tag. The problem I am running into is that the posts being displayed dont seem to be matching with the parents post tags.
    Posts on our site can have multiple tags(Currently developing a system to change this) and on average have about 4 tags. I thought I understood how the tags work by grabbing the tags from a post and popping them into an array. I then use the tags in the array in a new query to output posts with the same tags. My code for related tags is below:

    <?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(); ?>

    *Please note that $tags is an array of tags.
    I use the following code to store the tags from the post:

    if ( have_posts() ) : while(have_posts() ) : the_post();
    $tags = get_the_tags();
    global $post;

    I then pass this data into the code at the top using this method:
    <?php include(locate_template('related-posts.php'));//pass tags into related posts ?>

    Any help will be greatly appreciated

Viewing 3 replies - 1 through 3 (of 3 total)
  • what show if you output $tags at the start of your template?

    Thread Starter zaydB

    (@zaydb)

    Well Im using debug-objects plugin that shows all queries running and results if I want. This shows the tags in the form of an array. I haven’t tried to output the tags onto any html as yet.

    Thread Starter zaydB

    (@zaydb)

    Hi Michael,

    I tried to output the tag information but its empty… I guess the tags aren’t being stored in the variable correctly or not being passed into the template part correctly.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Retrieving Posts With Same Tags As Parent’ is closed to new replies.