• Hi! I’ve just created an WPMS installation with two sub-sites. They both share the same theme, and in one page, the user should be able to insert a post into the current site.

    I’m trying to use the function switch_to_blog() to switch to the current blog and then insert the post with wp_insert_post() and update_post_meta() but the posts are always inserted into the main site.

    Anybody knows what am I doing wrong? Here’s the code:

    $title = $_POST['title'];
    $author_id = $_POST['author_id'];
    $reference = $_POST['reference'];
    $blog_id = $_POST['blog_id'];
    
    $my_post = array(
     'post_title' => $title,
     'post_status' => 'pending',
     'post_author' => $author_id,
     'post_type' => 'order'
    );
    
    switch_to_blog($blog_id);
    $new_id = wp_insert_post( $my_post );
    update_post_meta($new_id, "reference", $reference);
    restore_current_blog();

    Thanks in advance!

Viewing 3 replies - 1 through 3 (of 3 total)
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘wp_insert_post in current blog’ is closed to new replies.