When user open any post, then on the right side they can see related posts
to that post. But currently in related posts
my current open post also showing, how i can exclude
that post which is currently open.
When user open any post, then on the right side they can see related posts
to that post. But currently in related posts
my current open post also showing, how i can exclude
that post which is currently open.
2 Answers
Reset to default 3Use the parameter 'post__not_in'
to exclude post IDs:
$posts = new WP_Query(
array (
'post__not_in' => array(get_the_ID()) // exclude current post ID
)
);
If you could give us some code snippets of what you are currently using, it would help us formulate an answer. Unfortunately I don't have enough reputation to say this as a comment on the OP, so I will provide my answer based on the knowledge provided in the OP.
When loading the current post (presumably by using single.php
), you can save the post ID in a PHP variable, like so:
<?php $do_not_duplicate = $post->ID; ?>
Later, in your loop to display related posts, you need an if-statement to skip the loop if the ID's match. Like so:
<?php
// Get related posts
$related = new WP_Query( $args );
while ( $related->have_posts() ) : $related->the_post();
if (in_array($post->ID, $do_not_duplicate)) continue;
?>
<!-- Your html for related post listing -->
<?php
endwhile; // End 'while have_posts()'
wp_reset_postdata(); // Reset Post Data
?>