Creating a post using PHP in one of the blogs in WP MU

I have a PHP program to create a site/blog in a networking enabled WordPress. After the site creation, I want to create a post using the same PHP program.

If I use wp_insert_post() function, it’s creating the post in the main site/blog, not in the new site/blog I created. I also tried using switch_to_blog() before calling the wp_insert_post() but no luck.

Related posts

Leave a Reply

2 comments

  1. The following used as Must Use plugin does the job:

    <?php
    /* Plugin Name: New Post on Site Creation */
    
    add_action( 'wpmu_new_blog', 'default_post_so_5334372', 10, 6 );
    
    function default_post_so_5334372( $blog_id, $user_id, $domain, $path, $site_id, $meta )
    {
        switch_to_blog( $blog_id );
        $my_post = array(
          'post_title'    => 'My post',
          'post_content'  => 'This is my post.',
          'post_status'   => 'publish',
          'post_author'   => 1
        );
        wp_insert_post( $my_post );
        restore_current_blog();
    }