How to Exclude the Current Post from the Latest Posts Widget in WordPress

You’ve probably seen this before 👀

You’re reading a blog post, scroll down…
And the Latest Posts widget shows the exact same post you’re already on.

Not broken, just awkward.

WordPress doesn’t give us a native option to exclude the current post from widgets. But with a simple PHP snippet added via WPCode Lite, we can fix this in minutes, no plugin upload needed.

Let’s do it the smart way 💡

Why This Happens

The default Latest Posts widget runs its own query.
It doesn’t automatically know which post you’re currently viewing, so it includes everything, even the current post.

That leads to:

  • Repetitive content
  • Confusing navigation
  • Slightly unpolished UX

We’re about to clean that up.

The Solution (WPCode Lite + PHP Snippet)

Instead of creating a standalone plugin, we’ll use WPCode Lite to inject a PHP snippet that:

  • Hooks into pre_get_posts
  • Runs only on single post pages
  • Targets widget queries only
  • Excludes the current post using post__not_in
  • Leaves the main query completely untouched

Zero risk. Zero clutter.

PHP Snippet (Add This in WPCode Lite)

Add a new PHP snippet in WPCode Lite and paste this code:

<?php
/**
 * Plugin Name: Exclude Current Post from Latest Posts Widget
 * Description: Automatically excludes the current post from appearing in the Latest Posts widget
 * Version: 1.0
 * Author: Syed Hussaini - MyKnowledgeBuddy.com
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Exclude current post from widget queries
 */
function exclude_current_post_from_widgets($query) {
    // Only run on frontend, not admin
    if (is_admin()) {
        return;
    }

    // Only modify widget queries (not main query)
    if (!$query->is_main_query()) {
        // Check if we're on a single post page
        if (is_singular()) {
            $current_post_id = get_queried_object_id();
            
            // Exclude current post from the query
            $post__not_in = $query->get('post__not_in');
            
            if (!is_array($post__not_in)) {
                $post__not_in = array();
            }
            
            if (!in_array($current_post_id, $post__not_in)) {
                $post__not_in[] = $current_post_id;
                $query->set('post__not_in', $post__not_in);
            }
        }
    }
}
add_action('pre_get_posts', 'exclude_current_post_from_widgets');

That’s all the logic you need.

How to Add This Using WPCode Lite

Quick steps 👇

  1. Go to WordPress Admin → Code Snippets → Add Snippet
  2. Choose PHP Snippet
  3. Paste the code above
  4. Set Run Snippet to:
    • ✔ Run Everywhere
  5. Activate the snippet

Done ✅

From now on, the current post will be automatically excluded from the Latest Posts widget.

Why This Approach Is a Win

  • No extra plugin files
  • Easy to enable/disable
  • Safe during theme changes
  • Perfect for production sites
  • WPCode Lite keeps everything organized

Honestly? This is how small WordPress tweaks should be done.

Final Thoughts

This is a tiny fix, but it levels up your site’s polish instantly.

If you want, next we can:

  • Limit this snippet to specific widgets
  • Exclude posts only from sidebar widgets
  • Make it work for custom post types
  • Adapt it for block widgets

Just tell me what you want to tweak next 🔥

Syed Hussaini
Syed Hussaini

With over 20 years of experience, I specialize in customer support, website development (using platforms like WordPress, Joomla, and Google Sites), IT operations, and digital asset management. I've managed teams, trained staff, and created Knowledgebase and Playbook articles. I’m known for my high productivity, independence, and strong communication skills, and I have successfully worked across diverse regions, including APAC, EMEA, Eastern time and AMER.

Leave a Reply

Discover more from My Knowledge Buddy

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from My Knowledge Buddy

Subscribe now to keep reading and get access to the full archive.

Continue reading