0

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 
    )
)

2 Answers 2

1

$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.

0
0

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)
2
  • 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. Commented Feb 4, 2012 at 15:04
  • Yup, $rows is the full array. I've not tested the above but it should work. Commented Feb 4, 2012 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.