WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

what would be the best way to exclude the current post I am viewing from this recent posts query. Thank You!

            global $post;
            if (in_category('top-lists')) {
                $myposts2 = get_posts('numberposts=5&offset=0&category=7');
            else if (in_category('playlists') || in_category('playlistall')) {
                $myposts2 = get_posts('numberposts=5&offset=0&category=6,37');
            else if (in_category('news') || in_category('news')) {
                    $myposts2 = get_posts('numberposts=5&offset=0&category=95');
            else {
                $myposts2 = get_posts('numberposts=5&offset=0&category=-6,-7,-37,-95,-177');

            foreach($myposts2 as $post) :
share|improve this question
up vote 4 down vote accepted

This the post__not_in arg should work dandy for you:

$args = array(
    'numberposts' => 5,
    'offset' => 0,
    'category' => 7,
    'post__not_in' => array( $post->ID )
$myposts2 = get_posts($args);
share|improve this answer
@kaiser and Brian - Thanks for getting back to me, I put in the code but I am getting a map_array error ---- Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/sitemain/public_html/wp-includes/query.php on line 1709 Warning: implode() [function.implode]: Invalid arguments passed in /home/sitemain/public_html/wp-includes/query.php on line 1709 – Chad Sep 19 '11 at 17:38
Try the latest edit. Populate your arguments into an array. – Brian Fegter Sep 19 '11 at 18:01
Hm? Why me? @Brian Fegter answered. :) – kaiser Sep 19 '11 at 18:08
ha :) I'm good. Always glad to help out. – Brian Fegter Sep 19 '11 at 18:22
Perfect, thanks everyone especially @BrianFegter ! – Chad Sep 21 '11 at 3:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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