Exclude Current Post from Recent Posts Loop

Quick Summary
- Pass array($post->ID) to the post__not_in argument of get_posts() so a recent posts list leaves out the post being read.
- The example sets numberposts to 5, offset to 0 and category to 7; change the category ID or remove it to include all categories.
- Use it in single.php; inside your own function declare global $post or use get_the_ID(), and call wp_reset_postdata() after setup_postdata() loops.
On a single post page, a recent posts list often includes the very post the visitor is reading. The post__not_in argument fixes that: pass it the current post’s ID, as in 'post__not_in' => array( $post->ID ), and WordPress leaves that post out of the results.
$args = array(
'numberposts' => 5,
'offset' => 0,
'category' => 7,
'post__not_in' => array( $post->ID )
);
$myposts2 = get_posts($args);How it works
numberpostssets how many postsget_posts()returns, here 5.offsetskips that many posts from the start; 0 starts with the newest.categorylimits the list to the category with ID 7. Change it to your own category ID, or remove the line to include all categories.post__not_intakes an array of post IDs to exclude.$post->IDis the ID of the current post.
Use the code in single.php or another template that runs on the post page. $post is the global post object, so inside your own function declare global $post; first or use get_the_ID() instead.
get_posts() returns an array of post objects in $myposts2. Loop over it with foreach to print each title and link, and if you use setup_postdata() for template tags inside that loop, call wp_reset_postdata() afterwards. A complete loop of that kind is in showing recent posts with get_posts().
Related articles
- related posts by tag that skip the current post: the same exclusion in a WP_Query
- related posts for a custom post type: posts that share the current post’s term

