4

I have a function that returns a comma separated list of post ids that a particular user can access. I want to use this list in a WP_Query loop.

The custom function:

$array = user_albums();
foreach( $array as $post ) {
    if( !in_array( $post->ID, $array ) )
        $ids[] = $post->ID;
}
$access_ids = implode( ', ', $ids );

So here is the situation:

  1. On my test site the id list is 158, 162, 145, 269.
  2. Inserting the list of ids returns only the first post. 'post__in'=> array( $access_ids ),
  3. Inserting the list of ids not in an array returns an error. 'post__in'=> $access_ids ,
  4. Inserting the post ids manually returns correct posts 'post__in'=> array( 158, 162, 145, 269 ),

What could I be doing wrong?
I appreciate any help.

  • Thanks guys for the input! The implode array was indeed causing the issue. – m-torin Feb 6 '12 at 20:34
4

$access_ids is a string. post__in accepts an array.

So instead of $access_ids you could use 'post__in'=> $ids skipping the $access_ids = implode( ', ', $ids ); all together.

  • for me this also not work can you please help me? – Niket Joshi Jan 2 '19 at 10:37
5

That implode() is probably what breaks things:

$access_ids = '158, 162, 145, 269';

$array = array($access_ids); //wrong
var_dump( $array ); 
// array
//  0 => string '158, 162, 145, 269' (length=18)

$array = array_map( 'trim', explode( ',', $access_ids ) ); // right
var_dump( $array ); 
//array
//  0 => string '158' (length=3)
//  1 => string '162' (length=3)
//  2 => string '145' (length=3)
//  3 => string '269' (length=3)
1

Just set 'post__in'=>$ids, declaring array( $access_ids ) doesn't create the desired array.

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.