I finally found the answer myself. It’s two parts:
- I need the global
$switchedto be defined. - The variable for blog id can’t be
$blog_idbecause 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();
Related Posts:
- WPML with WP_Query serving up all 3 languages [closed]
- Custom Post Type, WP_Query and ‘orderby’
- Templates for Custom Post Types and Custom Taxonomies
- Multisite wide post type?
- Querying Posts by Taxonomy From Alternate Network Site
- How do I Use Nested Loops of Custom Post Types for MultiSite Blogs using WP_Query()
- Rewrite Custom Post Type URL slug
- Multisite Global Custom Posts
- Multisite with a single, shared custom post type, while retaining site URL
- Multisite custom post type’s single page 404
- Sub-Sub-Blogs — creating and importing content into a custom sub-type
- Custom Post Type vs. User Page
- Remove default post types on all sites except primary blog. WPMU
- Redirect Single Post CPT (Custom Post Type) to a specific URL
- Plugin for custom posts and fields [closed]
- Custom Taxonomy Invalid in REST API
- wp_insert_term in a multisite installation
- How to set up multisite for multi language?
- How to build custom route by adding taxonomies to URL ? ex: www.demo.com/communities/palo-alto/
- WPMU: Cross site custom post type
- Displaying All Posts of a Custom Post Type in WordPress Multisite Backend
- Multiple pages for posts?
- How can you use one database with multisite
- Multisite: How to define and limit a Custom Post Type to specific sites?
- Flush Rewrite Rules when new site is created on network
- Assigning alternate single-{cpt} template based on blog_id in multisite
- WordPress multisite – is it possible to have different taxonomies for each site?
- Is there SEO or related limitations to using Custom Post Types instead of WPMU?
- How to migrate Custom Field Template to a WordPress Multisite
- posttype and custom fields on multisite
- Using custom post types WordPress network
- Two-way synchronizing of a post type among multisite blogs
- Custom Post type not Querying in switch_to_blog loop
- Adding a Section for Visitors
- Custom Post – additional param in the custom post URL goes to error page
- Main site single-property.php design, as homepage of a multisite
- Shared Custom post type between WP network sites
- Multisite – Echo admins profile meta
- install.php Custom Taxonomy Term not being added to custom post
- Fatal Error WordPress Custom Post Type UI + WPML
- Create WP multisites as a custom post type
- WordPress Multisite Network Shared Custom Post from Main Site using single-CPT.php
- Custom post type single page fine for admin, 404 from front
- Can Multisite and Custom Post Types work this way?
- Multisite permalink issue
- Adding a new custom post type using the editor causes 502 bad gateway error
- WP insert post and custom taxonomy
- Assign a Custom Role to a Custom Post?
- echo value from ‘select’ field type into page template using cmb2?
- Exclude add_filter from the admin
- Undefined property: stdClass::$labels in general-template.php post_type_archive_title()
- Genesis Child and Custom Post Type
- Separate Custom Post Type Taxonomy by Comma
- CSS for Custom Post Types
- Not sure why my custom post meta field isn’t saving
- Remove taxonomy slug when not assigning taxonomy with custom post types
- Add extra parameters after permalink?
- Return array of categories to php function for current post
- How do I get the permalink structure to work like this?
- Hierarchical Custom Post Types – Show only parent on tax archive?
- How to show customized activity for custom post types in buddypress activity loop?
- Sorting multiple custom post types without a meta key/value pair by sort order
- Remove fields on media uploader for custom page type – not working for “From Computer” or “From Url” tabs?
- wp_query check if integer exists in custom field’s array
- WP Storing postmeta into revisions
- How to clone Media custom post type?
- WordPress custom taxonomy, posts urls
- Import data from CSV with custom taxomony empty
- Custom Post Type Loop with Query Var
- Is there a way to establish parent/child relationship in WP menu links exclusively?
- Custom filter function not working with Custom post type
- How can I add a filter to a particular post format?
- How do you make a custom post type items automatically delete items 3 months after publication?
- get_post_meta is not working inside shortcode
- False Positive on has_post_thumbnail
- Enable comments on custom page type – option not showing in “customize” for theme
- How can I lock specific posts (cpt’s) from editing by anyone other than ADMINS
- How do I Query 2 Custom Post Types?
- Function to erase every post from a taxonomy
- Custom Post Type or Shortcode, What to choose? [closed]
- Building a Data Intensive Website with WordPress
- Use Author Ids or Names to echo different output inside loop
- Truncate custom post type content
- Group episodes by seasons
- Why this didn’t work if i use get_the_ID(), but works if i pass the numeric id
- click tags for custom post types
- Custom post type URL structure with site.com/custom_taxonomy_slug/post_name
- My CPT has archive false, now I need use parent Page for slug
- Links Image in “Catalog” Page to Posts
- ACF saving posts
- WordPress query posts by custom post type not workng
- Structuring 3K pages in WordPress
- How can I detect a chosen Isotope layout option from within Javascript to generate appropriate layout?
- How can i embbed two different galleries in custom post type
- No Current Class showing in Menu for Custom Post Types
- How can i display on front page a movie that is atached in a post type
- how to implement club post type
- Complex Custom Loop with Includes
- Custom Post type with a sub-URL under another Custom Post Type
- How to display the description of a custom post type in the dashboard?