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:
- On my test site the id list is
158, 162, 145, 269
. - Inserting the list of ids returns only the first post.
'post__in'=> array(
$access_ids ), - Inserting the list of ids not in an array returns an error.
'post__in'=> $access_ids ,
- 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.
$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.That
implode()
is probably what breaks things:Just set
'post__in'=>$ids
, declaring array( $access_ids ) doesn’t create the desired array.