• Hi Everyone,

    I am very new to WordPress Development. I am in mind 4th day so far, so I still don’t know a lot. I wonder if you can help me. I am editing single.php theme template. I want to add Related Posts section that references links to blog articles that have tags similar to one of the current blog article. The output would look like this:

    Current Article Name
    Some content of the current Article Name

    Related Posts:
    One Related Article
    Two Related Article

    Current Article Name should NOT be referenced under Related Posts, yet it does. Here is the code of single.php:

    <main id="main" class="site-main" role="main">
    			<?php if (have_posts()):?>
                	<?php while(have_posts()) : the_post(); ?>
                    	<?php the_title('<h1>','</h1>',true); ?>
                        <p class="auth">Written by <?php the_author_posts_link(); ?> on <?php the_time('F j, Y'); ?></p>
                        <p>Found in: <?php the_category(', '); ?></p>
                        <p><?php the_content(); ?></p>
                        <?php the_tags('<p>Tags: ',' / ','</p>'); ?>
                        <?php
                        //get an array of tags associated with this post
    					$tags = wp_get_post_terms(get_the_ID());
    					?>
                        <pre>
                        	<?php print_r($tags); ?>
                        </pre>
                        <?php
    					if ($tags)
    					{
    						echo '<h2>Related Terms:</h2>';
    						foreach ($tags as $tag)
    						{
    							$firstTag = $tag->term_id;
    							$args = array(
    							'tag__in'=>array($firstTag),
    							'post__not_in'=>array($post->ID),//it has $post->ID in the book. I used a different way to retrieve current post id
    							'posts_per_page'=>5,
    							'ignore_sticky_posts'=>1
    							);
    							//create nested custom Loop
    							$relatedPosts = new WP_Query($args);
    							if ($relatedPosts->have_posts())
    							{
    								while($relatedPosts->have_posts())
    								{
    									$relatedPosts->the_post();
    							?>
    								<p><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></p>
    							<?php
    								}
    							}
    						}
    					}
    					?>
                    <?php endwhile; ?>
                <?php endif; ?>
    		</main><!-- .site-main -->
Viewing 3 replies - 1 through 3 (of 3 total)
  • What happens when you:

    <pre>
          	<?php print_r($args); ?>
    </pre>

    Just before:
    $relatedPosts = new WP_Query($args);

    Thread Starter miami99

    (@miami99)

    Nothing really. I used it for testing just to see what’s inside of the arguments array.

    I know what print_r does.
    What I was wondering was is the $post->ID and other arguments correct ?
    Should you be capturing the value of get_the_ID() higher up in the loop ?
    Also do you need to do anything to close off the new query started by: $relatedPosts = new WP_Query($args); prior to fetching the next post in the outer loop ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Not Sure Why 'post__not_in' Is Not Working – Nested Loops’ is closed to new replies.