2

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?

  • Just check the type of the option. If array, put as is, if not then explode the commas (explode(',', $option)).. Can be confusing if you don't dump your data initially.. – Samuel Elh May 25 '16 at 23:35
  • The database "option" is just text, saved from a form field in theme options. But it appears I do need to sanitize it. – markratledge May 26 '16 at 4:59
1

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.

  • Ah, I think that's it; I'll try it. Similar: wordpress.stackexchange.com/questions/43267/… – markratledge May 26 '16 at 4:58
  • Using "include" as Gareth's answer works. Is there an advantage to using wp_parse_id_list ? – markratledge May 26 '16 at 17:21
  • The include parameter is a wrapper for post__in, that uses wp_parse_id_list(), with an adjustment in posts_per_page.Yes that will work. You should validate the database output, e.g. to make sure it's not empty, because empty post__in or include will not make get_posts() return an empty array. Another thing to consider: Should we always trust the core to sanitize the user input data or should we take care of it ? The latter sounds more responsible ;-) @markratledge – birgire May 26 '16 at 21:09
0

get_posts doesn't have a post__in "variable", that's in wp_query, in get_posts (or get_pages) you would need to use include e.g.

$news  = get_posts(
 array(
  'post_type' => 'post',
  'include' => $newspostids,
 )
);

https://codex.wordpress.org/Function_Reference/get_posts

  • But get_posts() is just a wrapper for WP_Query(), where in fact include is translated into the post__in argument with the help of wp_parse_id_list(). – birgire May 26 '16 at 0:07
  • oh right, I am just going on the codex and I would personally test that before anything else – Gareth Gillman May 26 '16 at 0:19
  • Any argument that is valid for WP_Query can be passed to get_posts – Pieter Goosen May 26 '16 at 5:00
  • This works; and I don't see any php errors in the logs. – markratledge May 26 '16 at 17:20

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.