how to exclude posts on current page from recent posts sidebar widget

I am using WordPress 3.6. I would like to exclude posts on any given current page from appearing in the recent posts sidebar widget. I haven’t been able to find a custom widget that has that option. I hope someone can help.

Related posts

1 comment

  1. Add this to the functions.php theme (or child theme) file.

    add_filter( 'widget_posts_args', 'wpse_109484_recent_post_count' );
    
    function wpse_109484_recent_post_count( $args ) {
        global $wp_query;
    
        $excluded_posts = array();
        foreach ( (array) $wp_query->posts as $post ) {
            $excluded_posts[] = $post->ID;
        }
    
        $args['post__not_in'] = $excluded_posts;
    
        return $args;
    }
    

Comments are closed.