I have the following code:

global $post;
$current_post = $post->ID;
$posts = get_posts('numberposts=5&post__in=' . $array . '&category='. $category->term_id . '&exclude=' . $current_post);

The error appeared is Warning: array_map() [function.array-map]: Argument #2 should be an array in /home/todaytra/public_html/depmua/wp-includes/query.php on line 2159

Could any one point out the mistake with my $array valiable?

Thanks for any help :)

  • Where are you defining $current_post? – Evan Yeung Oct 27 '12 at 6:00
  • I have edited the question – Pam Apple Oct 27 '12 at 6:05
  • @imHavoc It works perfectly without the second argument, meaning that the $current_post variable is correctly defined – Pam Apple Oct 27 '12 at 6:12
  • What is the value being returned by $current_post? – Adam Oct 27 '12 at 7:06
  • When i echo $current_post it gives 213629 . Without the post__in argument i get nicely 5 posts, some have id larger than 213629, some smaller. By the way, i don't have too much posts (i deleted a lot of post revisions and drafts, that's why i have that big ID) – Pam Apple Oct 27 '12 at 7:15

I have just found an alternative solution:

global $post;
$current_post = $post->ID;
$args = array(
'numberposts'     => 5,
'offset'          => 0,
'category'        => $category->term_id,
'orderby'         => 'ID',
'order'           => 'DESC',
'exclude'         => $current_post,
'post_type'       => 'post',
'post_status'     => 'publish',
'suppress_filters' => true,
'post__in' => $array );

$posts = get_posts($args);

I'm not sure why the previous method does not work. I have resolved the problem anyway. Thank @imHavoc and @userabuser for having taken a look at my problem :)

  • That's because you're not passing an array of values into get_posts in your OP. The syntax you described above is the correct syntax. See this thread for further understanding. – Adam Oct 27 '12 at 8:35
  • also you can use get_the_id function i think instead of $post->ID. link – Fatih Toprak Oct 27 '12 at 9:21
  • array is a PHP keyword, I would advise you choose a different name for your variable – Tom J Nowell Mar 22 '13 at 16:01
  • @pamapple please check the tick mark on the left to the answer to mark your answer accepted. – Mayeenul Islam Jun 13 '15 at 1:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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