Using wp_insert_post() with Networking enabled
-
16-10-2019 - |
Question
I'm working on a script that will drop articles directly into the wordpress database, using wp_insert_posts(). However, I have networking enabled and the wp_insert_post() reference page has no documentation relating to using wp_insert_post() to upload to a specific site on the network.
How can I do this with wp_insert_posts()? Or is their another function which does this?
Solution
When using function like wp_insert_post()
It will work on the currently active blog, meaning that if you are on a blog in your network with blog_id of 2 the the post you insert using wp_insert_post()
will be inserted to that blog.
Now if you want to insert the post to a specific blog you can use switch_to_blog()
function which will tell WordPress that "the current blog is" so you can use wp_insert_post()
to insert to that specific blog.
Then to tell WordPress that what is the real current blog you can use restore_current_blog()
so:
switch_to_blog($wanted_blog_ID);
...
...
your wp_insert_post() stuff
...
...
restore_current_blog();
take a look at WPMU Functions to understand more about the available functions in a network.