7

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?

  • 1
    Why not to use switch_to_blog in order to insert your posts? – konus Apr 29 '11 at 14:32
9

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.