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.

Working on a directory listing and have cobbled together the shortest query I can. It appears to return the listing ok (I think) but the foreach is somehow failing to process the array properly. Can anyone give me a tip on what I'm doing wrong?

global $wpdb; 
$site_blog_ids = array($wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs     where blog_id > 1"))); // get all subsite blog ids

print_r( $site_blog_ids ); // checkem - output is "Array ( [0] => Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) ) "

foreach( $site_blog_ids as $site_blog_id ) { //iterate through the ids
print_r( "siteid ".$site_blog_id ); // checkem - this just outputs "array array" ?? 

The blog IDs aren't coming through the foreach properly somehow, it's just outputting "array array" whereas it should show the list of blog IDs. Does this look like a problem with the $site_blog_ids array or did I mess something else up?

Thanks for any help! David

V.2 per MBoynes' help. Fixed the double array because wpdb already outputs an array. But it's still not working at the foreach to ouput the $site_blog_id, and changing to "$site_blog_ids[0] AS $site_blog_id" only outputs the first one in the array and then stops.

global $wpdb; 
$site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs where     blog_id > 1")); // get all subsite blog ids

print_r( $site_blog_ids ); // checkem - output is "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) "

foreach( $site_blog_ids AS $site_blog_id ) { //iterate through the ids
print_r( "siteid= ".$site_blog_id."</br>" ); // checkem - this shows no blog ids output at all ??

v.3 Working version for posterity global $wpdb; $site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs where blog_id > 1")); // get all subsite blog ids

print_r( $site_blog_ids ); // checkem - output is "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) "

foreach( $site_blog_ids AS $site_blog_id ) { //iterate through the ids
print_r( "siteid= ".$site_blog_id->blog_id."</br>" ); // checkem - anything in the loop that needs the blog ID value must pull it with the ->blog_id key. 
share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

You're setting $site_blog_ids to be an array of arrays. In that case, you'd want to do foreach ($site_blog_ids[0] as...

share|improve this answer
    
hmm ok I gotcha, wpdb already returns array. I fixed that and now the output is "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) )" so that looks good. –  Dains Feb 15 '12 at 3:41
    
Yep, exactly. Working now? –  Matthew Boynes Feb 15 '12 at 3:55
    
No sadly, still not getting anything for $site_blog_id, and adding [0] only outputs the first one in the array and doesn't do the rest. –  Dains Feb 15 '12 at 4:02
    
OK, you're close! Now you want to do $site_blog_id->blog_id in your print_r and you'll be golden. –  Matthew Boynes Feb 15 '12 at 4:11
    
no the print_r's are just to check the output to see what's failing, sorry for the confusion. I've fined it down to the "$site_blog_ids = " because "print_r($wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs where blog_id > 1")));" outputs the array just fine, but $site_blog_ids doesn't show anything on print_r. I don't understand at all, there's tons of examples on wpdb and they all look like this. –  Dains Feb 15 '12 at 4:36
show 8 more comments

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.