tags:

views:

90

answers:

3

Hey Guys,

I have an array called $array_all;

This array will always have 3 possible values:

  • 1,
  • 1,2,
  • 1,2,3,

I created a string from the array while in a foreach loop and concatenated a comma at the end.

So, now I have a nice string that outputs the exact value the way it should.

1,2,3, I can copy this output from my browser and insert it into my wordpress function and everything displays perfectly.

The problem arises when I insert this string variable in the wordpress function directly, it fails.

Anybody have any ideas?

Code below:

<?php 
$faux_array = array();
$faux_array_all;

if($five_loans != ''): 
    $faux_array[] = "781";
endif; 
if($logbook_loans != ''): 
    $faux_array[] = "797";
endif;
if($easy_money != ''): 
    $faux_array[] = "803";
endif;

foreach($faux_array as $faux_array_value): 
    $faux_array_all .= $faux_array_value . ',';
endforeach;

echo $faux_array_all;

$args = array
(
    'posts_per_page' => 10,
    'post_type' => 'lender',
    'order' => 'ASC',
    'orderby' => 'date',
    'post__in' => array($faux_array_all)
);
?>
+1  A: 
'post__in' => $faux_array

Try this, and if it doesn't work post the code that you manually make to work please.

Edited. Check it now.

Alin Purcaru
Thanks, but it's the same result as mine. It only putputs one result. This works: 'post__in' => array(781,797,)
Keith Donegan
+1  A: 

You need to trim off the trailing comma

foreach($faux_array as $faux_array_value): 
    $faux_array_all .= $faux_array_value . ',';
endforeach;
if (substr($faux_array_all)-1,1) == ",") {
$faux_array_all = substr($faux_array_all,0,-1);
}
Liam Bailey
Still works without it and not the original problem but thanks for replying.
Keith Donegan
+3  A: 

Mmh for one, you can avoid the loop with just:

$faux_array_all = implode(',', $faux_array);

which would also solve the trailing comma proble.m.

On the other hand, you pass an array to post__in that only contains one element (the string). I think what you really want is

'post__in' => $faux_array

as $faux_array is already an array with IDs.
Read about Post & Page Parameters, there you can see that you need to pass an array of post IDs to the function, not an array with one string value:

  • 'post__in' => array(5,12,2,14,7) - inclusion, lets you specify the post IDs to retrieve
Felix Kling
Bingo!! Felix, thanks a million mate!
Keith Donegan