• I have a sidebar that is pulling in related blog posts by tags but it is showing the current post in the sidebar instead of just pulling two random posts.

    Sample page: http://staging.rswcreative.com/four-signs-that-mobile-learning-is-on-the-rise/

    This is the code that I think needs to be edited.

    <aside id="news-entry-sidebar">
               <div id="related-case-header">Related Case Studies</div>
    		<?php
    		$posttags = get_the_tags();
    		$test = '';
    		$sep = '';
    		if ($posttags) {
    			foreach($posttags as $tag) {
    				$test .= $sep . $tag->name;
    				$sep = ",";
    			}
    		}
    		query_posts('tag=' .$test . '&showposts=1'); while (have_posts()) : the_post(); ?>
    		<div class="post-page-work-entry">
    			<?php the_post_thumbnail(); ?>
    			<p style="line-height: 110%;"><?php the_field('client'); ?><br />
    			<?php the_title(); ?><br />
    			<span style="color: #888888; font-size: 16px;"><?php the_field('project_type'); ?></span></p>
    			<p class="post-page-work-entry-excerpt"><?php the_excerpt(); ?><a href="<?php the_permalink(); ?>"></a></p>
    		</div>
    
    		<?php endwhile; wp_reset_query(); ?>
    
            </aside>
Viewing 3 replies - 1 through 3 (of 3 total)
  • try post__no_in as an example below

    <?php
    $this_post = $post->ID;
    query_posts(array('cat' => $cat_ID, 'post__not_in' => array($this_post), 'posts_per_page' => 14, 'orderby' => 'rand'));
    ?>

    as in your case

    $this_post = $post->ID;
    query_posts('tag=' .$test . '&showposts=1', 'post__not_in' => array($this_post);
    Thread Starter cgriffis

    (@cgriffis)

    Not sure what part to replace. It looks like your example is using categories not tags.

    thanks

    Following is the argument we pass to exclude the current post while we query post. Query everything same as you doing with additional argument as

    ‘post__not_in’ => array($post->ID),

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

The topic ‘Exclude Current Post from Query’ is closed to new replies.