This must be fairly simple, but I’m puzzled. And I may be going about it backwards.
How do I output a database option that is an array into a get_posts array?
I have an option in the database that stores some post IDs in the array format `1234,5678′ . I can retrieve and echo them with
$options = get_option( 'pu_theme_options' );
$newspostids = $options['pu_textbox'];
echo $newspostids;
so I know am getting the output `1234,5678′. The database “option” is just text, saved from a form field in theme options.
What I need to do is get the data from that option into a get_posts
array so I can display the posts by ID.
What I need is for get_posts
to function like this:
$news = get_posts( array(
'post_type' => 'post',
'post__in'=> array(1234,5678)
) );
And, obviously, this doesn’t work, putting the variable directly into the array and expecting it to output `1234,5678′ :
$news = get_posts( array(
'post_type' => 'post',
'post__in'=> array($newspostids)
) );
So how should this work?
2 Answers
You could use wp_parse_id_list()
to sanitize your comma seperated list of post IDs. It’s defined as:
function wp_parse_id_list( $list ) {
if ( !is_array($list) )
$list = preg_split('/[\s,]+/', $list);
return array_unique(array_map('absint', $list));
}
where we can see that it returns unique array values as well.
Note that absint()
is defined in core as abs( intval() )
and there’s a difference in the max output of intval()
for 32 and 64 bit systems according to the PHP docs.