post__in for get_posts with a dynamic array

I am creating an RSS feed. I have created a table in my database with 400 post id’s. I have the rss feed coded I am now trying to link the two so my RSS feed runs only for the list of 400 posts from my table.

On my RSS feed page I can create an array by using a couple of id’s (see below)

$myarray2 = array(12345,12346);
$args = array(
'post_type' => 'posttype',
'post__in' => $myarray2
);

This works and creates an RSS feed for id’s 12345,12346.

I can also print the array of my 400 database table records on the page as follows;

$myarray = $wpdb->get_results("SELECT * FROM my_table");
print_r($myarray)

This produces data of this form

( 
        [0] => stdClass Object ( 
                [id] => 145 
            ) 
        [1] => stdClass Object ( 
                [id] => 4573 
            ) 
        [2] => continues.....
)

However I can not seem to get the code right to use $myarray with post__in for example

$myarray = $wpdb->get_results("SELECT * FROM my_table");
$args = array(
'post_type' => 'posttype',
'post__in' => $myarray
);

does not work. I have tried various variations but I am missing something about how to deliver a list of post id’s from my array to post__in

What am I missing?

3 Answers
3

Here’s one way by using wpdb::get_col() and fetch the id column:

$pids = $wpdb->get_col( "SELECT id FROM my_table" );

We could then clean it by integer convert each id with:

$pids = wp_parse_id_list( $pids );

Just note the different max value for intval(), depending on 32 or 64 bits systems.

Leave a Comment