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.
You’re setting
$site_blog_ids
to be an array of arrays. In that case, you’d want to doforeach ($site_blog_ids[0] as...