4

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?

  • 1
    $myarray in the last case is not an simple array with post ids , it's an array of rows resulted from sql query. So loop through that result and get ids into an array. – bravokeyl May 24 '16 at 7:48
  • I have tried to do that but I can't seem to get the syntax correct. Do you have any pointers about how to do it.=? – Jolo May 24 '16 at 8:39
1

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.

| improve this answer | |
1

post__in accept array of post IDs e.g. array(5, 4, 8, 9);

Extending @bravokeyl comment. By default result is Object. As codex suggest you can change the output type of get_results() to these values.

  • OBJECT - result will be output as a numerically indexed array of row objects.
  • OBJECT_K - result will be output as an associative array of row objects, using first column's values as keys (duplicates will be discarded).
  • ARRAY_A - result will be output as a numerically indexed array of associative arrays, using column names as keys.
  • ARRAY_N - result will be output as a numerically indexed array of numerically indexed arrays.

Here we can change output to ARRAY_A then extract post IDs and pass to post__in

global $wpdb;
$fivesdrafts = $wpdb->get_results("SELECT ID FROM my_table", ARRAY_A);
$post_ids = array_map(function($single_array){
    return $single_array['ID'];
}, $fivesdrafts);

//Pass this to post__in
array(
    'post__in' => $post_ids
);
| improve this answer | |
  • Thanks - however when I use that code structure I get this error "Cannot use object of type stdClass as array" – Jolo May 24 '16 at 8:38
  • You did not read my answer well :P You need to change the output type to ARRAY_A in get_results – Sumit May 24 '16 at 8:54
  • Using this global $wpdb; $jlk = $wpdb->get_results("SELECT * FROM my_table ARRAY_A"); $jlk2 = array_map( function($post) { return $post->ID; }, $jlk ); $args = array( 'post_type' => 'custom_post', 'numberposts' => 10, 'post__in'=> $jlk2 ); $results = get_posts($args); I don't get an error, I can print the array which looks like this (( [0] => stdClass Object ( [id] => 14578 ) [1] => stdClass Object ( [id] => 14573 ) ) but it does notwork in this location 'post__in'=> $jlk2 – Jolo May 24 '16 at 9:01
  • No! Function accept 2nd argument, separated by comma. See the example above. It just copy paste. ("SELECT * FROM en_notsold", ARRAY_A) and return $post['ID'] – Sumit May 24 '16 at 9:02
  • OK I'm getting closer, global $wpdb; $jlk = $wpdb->get_results("SELECT * FROM my_table", ARRAY_A); $jlk2 = array_map( function($post) { return $post->ID; }, $jlk ); $args = array( 'post_type' => 'custompost', 'numberposts' => 401, 'post__in'=> $jlk2 ); $result = get_posts($args); if I print_r the $jlk2 the id is missing, I only see the index (Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => etc ) so I need to see the post_id column. – Jolo May 24 '16 at 12:35
-2

OK I got it to work as follows.

global $wpdb;
$jlk = $wpdb->get_results("SELECT * FROM my_table");

foreach($jlk as $custompost) {
 $custompost_ids[] = $custompost->ID;
}

$args = array(
'post_type' => 'custompost',
'numberposts' => -1,
'post__in'=> $custompost_ids
);
$result = get_posts($args);
| improve this answer | |
  • 2
    Why are you fetching the whole table if you only need the ID column ? Looks like you could simplify more withwpdb::get_col() since it's made for selecting a single column and returns a single dimensional array. – birgire May 24 '16 at 14:02
  • Thanks for pointing that out, I need to make an edit here! – Sumit May 24 '16 at 14:59
  • There is only one column in the table. – Jolo May 26 '16 at 5:09

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.