You can use the function switch_to_blog() for this
$other_id = 1234 // the id of the other blog to save the post to
switch_to_blog($other_id);
$my_post = array(
'post_title' => $post_title,
'post_content' => $post_content,
'post_status' => 'publish',
'post_author' => $post_author,
);
// Insert the post into the database
wp_insert_post( $my_post );
restore_current_blog();
There is a pitfall if you execute this code on the save_post hook, because wp_insert_post also calls save_post and you end up in an infinite loop. This post on Stack Overflow gives a solution for that.
Related Posts:
- How can I have the posts from one word press blog show on the presentation page of another word press site
- Get image paths from RSS feeds
- How to Display Network Post Count?
- Disable WordPress’ blogging functionality and disable ‘posts’
- How to cache a shortcode functions output?
- Querying post from a multisite network
- How to get a post content from another wordpress blog/site?
- How do I convert a mathematical Microsoft Word document to a WordPress blog post?
- widget should display post archive by year and on click also by month
- how to enable different number of revisions for different blogs when using WPMU?
- Strange switch_to_blog() issue
- How to hook a function only when I need to delete permanently a post?
- How to create post in WP network using WP-CLI
- Side by side blog posts that are tied together
- Pull posts from another wordpress install on same server
- How to show a full post, not just an excerpt
- Query posts from different categories in multisite
- Custom excerpt length filter doesn’t work
- Limit Gutenberg blocks available to users to choose from
- Portfolio + Blog: multisite or plugin?
- Multisite – Get current post id
- Remove certain post-formats from showing in blog?
- Blog Posts in Custom Menu
- Why is my Blog Page ID == First Post ID?
- Displaying Latest Posts on a Page
- How to make number of blog posts a custom field?
- Choose whether to automatically add a taxonomy with the same name as the post
- How can i display gallery shortcode output under Post text
- WP Bakery Load More Button loads the same posts
- Append class to posts page
- Multisite: Can’t Post or Create Categories
- Unable to restore from backup – how to obtain old blog posts?
- Pagination with an array of post objects?
- Posting blog entries in screenplay format
- Set static page/post from another blog on same network
- Single.php – Get Current Parent Category
- How to remove in-line style of WordPress’s post content only?
- How to WP_Query() from multiple blogs and order them?
- Is it possible to filter the main loop to exclude posts from a specific category?
- Append ‘Continue Reading’ link if post length is more than 3 lines of text?
- How do I show posts from another wordpress installation?
- older blog posts not showing in new theme
- Why won’t pagination work?
- WordPress Query Posts From Category Post on Static Page
- How to get the latest URL of my blog?
- Un-highlight Blog Menu Item when Category Menu Item is Selected
- Exclude post category in a blog page
- Change Author Name to Sitename on Frontend
- Where is the “Posts”/”Blog” template?
- Cannot select certain authors for posts after import
- How to separate each individual blog post?
- Changing The Default Header POST Title and Description text by category in wordpress
- why my urdu text is not aligned properly when written in wordpress blog post?
- Create a List of Widgets, where each widget holds three attributes
- Pagination not give another posts
- “Blog pages show at most” in setting not working
- Sticky Post Doesn’t always work
- Single Post Gallery Using Shortcode
- Displaying all posts from other sites on the network on one site
- blog post not accesible on click [closed]
- How to fix wordpress blog post error? Can anyone help to fix this issue?
- How to pull sticky post permalink inside php?
- Multisite how to display merged posts from two sites and sort by latest date?
- Use posts from old db by changing prefix
- How to backup WP blog posts to computer, not new site
- Blog featured images disappeared on my homepage
- Blog images not showing on homepage
- Blog Page doesn’t show summary but full content
- How to generate an HTML link automatically from URL in a users’ post
- Get posts from category from custom query
- Populate a drop down list with post titles across a multisite network
- My posts page is missing the page title
- Blog Post slider not working
- Multiple Pages – Seperate blogs
- How to put the WordPress blog into theme
- Why the blog posts is not showing?
- Edit Posts Page but not category specific pages?
- Blog styling in SASS
- Link post to blog from a static page
- Hide first blog post in content and output it in widget
- How to get posts from network blog
- Page updates appear in Preview but not live version?
- Sorting Posts by Date – get_blogs_of_user_id()
- Users cannot post when editor or admin on more than 1 site on multisite
- How to link two news page multilingual press
- new blog post does not appear in blog after publishing
- Posts don’t appear on the Blog page
- making a few sites in wordpress
- 2 tick boxes appearing below comments
- Wanting to apply different featured images for each post
- WordPress Custom Theme: My site shows the same posts on Page 1 and Page 2 and so on [duplicate]
- Post only showing on homepage when Logged in – direct URL still working
- Posts not showing up on Blog page
- How to save the same post in multiple blogs?
- Do I need to create a multisite for querying posts from multiple WordPress sites?
- Need to show birthday of custom post type(Famous people in this case) in elementor. Date of birth set in custom fields
- Site ‘Categories’: save an admin global setting with post metadata [closed]
- users followin the blog post author
- Permalink issue with new blog posts > getting 301 redirect
- Blog page shows the whole blog post instead of the excerpt or summary with the feature image only