Not Sure Why 'post__not_in' Is Not Working – Nested Loops
-
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 NameRelated Posts:
One Related Article
Two Related ArticleCurrent 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)
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.