我试图遍历一系列站点,但似乎无法使set\\u blog\\u id正常工作。以下是示例输出:
数组([0]=>stdClass对象([blog\\u id]=>2)[1]=>stdClass对象([blog\\u id]=>3)[2]=>stdClass对象([blog\\u id]=>5))
从数组中获取新的\\u blog\\u id并检查值-blog\\u id=2
当前日志=5
使用set\\u blog\\u id将当前blog\\u id设置为new\\u blog\\u id
设置\\u blog\\u id后,get\\u current\\u blog\\u id=5-应为2
重置为oldblog get\\u current\\u blog\\u id 5后
正如您所看到的,除了set\\u blog\\u id之外,一切都正常。下面是代码。任何帮助都将不胜感激!
function return_blog_first_posts(){
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); /*output check - "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) "
echo "</br>"; */
foreach( $site_blog_ids as $blog_id ) { /* iterate through the ids */
print_r( "</br>new blog_id check - blog_id= ".$blog_id->blog_id ); /* output check - this shows blog id */
echo "</br>current_blog= ".get_current_blog_id();
$newblogid = $blog_id->blog_id; //
$oldblog = $wpdb->set_blog_id( $newblogid ); // this is not working
echo "</br> after set_blog_id, get_current_blog_id= ".get_current_blog_id();
}
/* Tell the $wpdb object to go back to using the current site */
$wpdb->set_blog_id( $oldblog );
echo "</br> after resetting to oldblog get_current_blog_id ".get_current_blog_id();
}
return_blog_first_posts();