• Resolved zaydB

    (@zaydb)


    Hey guys,

    Trying to get the post_not_in to work on a page that utilises related posts. So i can get the related posts displayed according to specific tags from the original post, it appears to work with the code I have, but I run into the into the issue of having the original post in the related posts section. Here is my code for displaying a single post:

    <?php
    /*
    Single Post Template: event
    */
    ?>
    <?php get_header(); ?>
    
    <?php
    global $post;
    
     if ( have_posts()) : while(have_posts()) : the_post();
    
     $link= get_post_meta(get_the_ID(),'Company Link',true);
     //$tags=wp_get_post_tags(the_post()->ID);//get array of tags
    $tags = get_the_tags();
    
    ?>
    
    <h2 class="page-title"><?php the_title();?></h2>
              <div id="post_content" class="post_content" role="main">
                <article class="type-post hentry">
                  <div class="post-info">
                    <div class="post_date">From: <?php echo get_post_meta(get_the_ID(),'Event Day Start',true);?> <?php echo get_post_meta(get_the_ID(),'Event Month Start',true);?> <?php echo get_post_meta(get_the_ID(),'Event Year Start',true);?> to <?php echo get_post_meta(get_the_ID(),'Event Day End',true);?> <?php echo get_post_meta(get_the_ID(),'Event Month End',true);?> <?php echo get_post_meta(get_the_ID(),'Event Year End',true);?></div>
                   <a href="post-standart.html" class="post_format"></a> <a href="<?php echo get_post_meta(get_the_ID(),'Comapny Link',true);?>" class="comments_count"><?php echo get_post_meta(get_the_ID(),'Comapny Link',true);?></a>
                  <div class="post_views"></div>
                  </div>
                  <div class="pic post_thumb"> <?php the_post_thumbnail(); ?> </div>
                  <div class="post_content">
                    <?php the_content(); ?>
                  </div>
                </article>
                <div id="post_author">
                  <h3>Location: <span style="font-weight:normal;"><?php echo get_post_meta(get_the_ID(),'Event Location',true); ?> </span></h3>
                  <div class="photo"><img alt="" src="<?php print IMAGES; ?>/Link.jpg"/> </div>
                  <div class="extra_wrap">
                    <h4><a href="<?php echo $link; ?>" target="_blank"><?php echo $link; ?></h4></a>
                    <div class="description"></div>
                  </div>
                </div>
    
    			<div id="recent_posts">
                  <h3 class="section_title"> Related Articles </h3>
                   <?php include(locate_template('related-posts.php'));//pass tags into related posts ?> 
    
                </div>
    
    				<?php endwhile;//Mainloop while end
    					endif;//Mainloop if end
                 wp_reset_postdata(); //
              ?>
    
    	</div>
    
    	</div>
    </div>
    
    <?php get_footer();
    ?>

    and here is my related posts template. Note that i used:
    include(locate_template('related-posts.php')); in order to use all variables available in the single post.

    Related Posts:

    <?php           $args=array('tag_in'=>$tags,
    							'post_not_in'=>$post->ID,
    							'post_per_page'=> 4,
    							'ignore_sticky_posts'=>1,
    							'post_type'=>array('events','news_article','opinions'));
    				$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 substr(get_the_title($rel),0,12); ?></a></h3>
                      <div class="post-info"> <a href="<?php echo get_permalink($rel);?>" class="post_date"><?php echo get_the_tags($post->ID) ?></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;
     				}

    Please let me know if my approach was done correctly and what I can do to improve this.

    Thanks 🙂

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

The topic ‘post_not_in’ is closed to new replies.