I finally found the answer myself. It’s two parts:
- I need the global
$switched
to be defined. - The variable for blog id can’t be
$blog_id
because that’s reserved.
The above code should thus be the following.
require_once( "../wp-load.php" );
global $switched;
$blog_id_target = 2;
switch_to_blog($blog_id_target);
$lang = 'en';
$new_pr = wp_insert_post(
array(
'post_author' => 1,
'post_status' => 'publish',
'post_type' => 'pressreleases',
'post_date' => $this->publish_date,
'post_content' => $this->body,
'post_title' => $this->title,
'tax_input' => array(
'pr_categories' => $this->pr_category_id
)
)
);
var_dump( get_current_blog_id() ); // This returns (int)2, just like it should.
restore_current_blog();