Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code:

global $post;
$current_post = $post->ID;
$array=range(1,$current_post);
$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 :)

share|improve this question
    
Where are you defining $current_post? –  imHavoc 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? –  userabuser 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

1 Answer 1

I have just found an alternative solution:

global $post;
$current_post = $post->ID;
$array=range(1,$current_post);
$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 :)

share|improve this answer
    
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. –  userabuser 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

Your Answer

 
discard

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.