1

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.

4
  • The built-in (WordPress Core) recent posts widget?
    – s_ha_dum
    Aug 8, 2013 at 22:48
  • Yes, the built-in recent posts widget as it's fine the way it is. I just want to exclude posts from a current page from it.
    – Nyx
    Aug 9, 2013 at 2:50
  • You can use the 'widget_posts_args' WordPress filter to add an array of excluded posts to the widget query. Aug 9, 2013 at 5:06
  • Thanks for responding but I didn't find anything that excludes a page, current or otherwise. Can anyone tell me what I'd need to put to do this?
    – Nyx
    Aug 9, 2013 at 11:12

1 Answer 1

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;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.