Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

What is the best php method to extract all IDs from a query string and display them in a comma separated list?

I have a function that grabs specific posts from the database and am trying to enter those ID's into a new wp_query loop.

Array (
    [0] => stdClass Object (
        [ID] => 162
        [post_author] => 1
        [post_date] => 2011-10-22 09:25:46
        [post_date_gmt] => 2011-10-22 14:25:46
        [post_content] =>
        [post_title] => Paper III
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] =>
        [post_name] => paper-iii
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2011-12-22 09:22:05
        [post_modified_gmt] => 2011-12-22 14:22:05
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http:/mysite.org/?post_type=album&p=162
        [menu_order] => 0
        [post_type] => album
        [post_mime_type] =>
        [comment_count] => 0
        [user_nicename] => matt
    )
    [1] => stdClass Object (
        [ID] => 166
        [post_author] => 2
        [post_date] => 2011-12-22 11:32:46
        [post_date_gmt] => 2011-12-22 16:32:46
        [post_content] =>
        [post_title] => mtayla's album
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => closed
        [post_password] =>
        [post_name] => mtaylas-album
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2011-12-26 20:23:00
        [post_modified_gmt] => 2011-12-27 01:23:00
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http:/mysite.org/?post_type=album&p=166
        [menu_order] => 0
        [post_type] => album
        [post_mime_type] =>
        [comment_count] => 0
        [user_nicename] => mtayla
    )
    [2] => stdClass Object (
        [ID] => 308
        [post_author] => 2
        [post_date] => 2012-01-31 15:04:29
        [post_date_gmt] => 2012-01-31 20:04:29
        [post_content] =>
        [post_title] => another album
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] =>
        [post_name] => another-album
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2012-01-31 15:04:29
        [post_modified_gmt] => 2012-01-31 20:04:29
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http:/mysite.org/?post_type=album&p=308
        [menu_order] => 0
        [post_type] => album
        [post_mime_type] =>
        [comment_count] => 0
        [user_nicename] => mtayla 
    )
)
share|improve this question

2 Answers 2

up vote 1 down vote accepted

$string will contain a comma separated list of IDs:

$ids = array();
foreach( $array as $post ) {
    if( !in_array( $post->ID, $ids ) ) {
        $ids[] = $post->ID;
    }
}

$string = implode( ', ', $ids );

Note: This uses basically no wordpress, other than the post object...which is really just an object.

share|improve this answer
    
Thanks @m0r7if3r! –  mattrepublic Feb 6 '12 at 12:34

This isn't really a WordPress question, but you could just use array_map to extract the IDs, but them in an array and implode that...

$id_array = array_map('my_id_extraction', $rows);
function my_id_extraction($item){
    return $item->ID;
}
$id_list = implode(',', $id_array)
share|improve this answer
    
So in your example $rows is the full array? I know that it is more php, but I posted it here because I thought it was applicable for others trying to extract info from wpdb. –  mattrepublic Feb 4 '12 at 15:04
    
Yup, $rows is the full array. I've not tested the above but it should work. –  Stephen Harris Feb 4 '12 at 15:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.