0

I'm creating new blogs (wpmu_create_blog) in a for loop,

What I am trying to achieve now is to insert posts into each newly created blog using switch_to_blog.

The problem is wp_insert_post still writes to the main site (which is not wanted) - while also writing to each new blog (which is wanted).

For example, there are 3 new sites that needs to be created, so the for loop iterates 3 times.

This is what is being created:

Main Site:
Subsite 1 - Page 1
Subsite 1 - Page 1 (created from the for loop)
Subsite 2 - Page 1 (created from the for loop)
Subsite 2 - Page 1
Subsite 3 - Page 1 (created from the for loop)
Subsite 1 - Page 1 (created from the for loop)
Subsite 3 - Page 1
Subsite 2 - Page 1 (created from the for loop)
Subsite 3 - Page 1 (created from the for loop)

Main Subsite (Intentional, hence why the function is using wpmu_new_blog):

Subsite 1:
Page 1

Subsite 2:
Page 1

Subsite 3:
Page 1

I'm not sure what's causing the issue.

<?php
  /* ----------------------------------------------------------------------------- *
   * Create New Sites w/ wpmu_create_blog and insert posts w/ switch_to_blog
   * ----------------------------------------------------------------------------- */
  function wpmu_app_sites( $blog_id, $user_id ) {

    // Used to get the primary site URL
    global $current_site;

    # Load WordPress barebones
    define( 'WP_USE_THEMES', false );
    require( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php' );

    # Multisite domain
    $main_site = $current_site->domain;

    # Type of Multisite
    $subdomain_install = true;

    $path = '/';

    // Get User ID
    $user_name  = $_POST['user_name'];
    $user = get_user_by('login', $user_name);
    $user_id = $user->ID;

    $siteList = $_POST['bootstrap']['siteList']; // Repeater List

    if ( !empty($siteList) ) {
      $count = count($siteList); // Get the count of how many rows are in the list

      // For each item (ie. row), run through this loop until there is no more rows ($key++ increment)
      for( $key = 1; $key < $count; $key++ ) {
        $siteId    = $_POST['bootstrap']['trackList'][$key]['trackId']; // App ID specific to that row (i.e. $key)
        $siteName  = trim($_POST['bootstrap']['trackList'][$key]['trackName']); // App Name specific to that row (i.e. $key)
        $post_title = ucwords($siteName);

        $domain = "$siteId.$main_site";

        $blog_id = wpmu_create_blog($domain, $path, $post_title, $user_id);

        switch_to_blog($blog_id);

        $post = array(
          'post_name'   => sanitize_title( $post_title ), // Post Slug
          'post_status' => 'publish',
          'post_title'  => $post_title, // Post Title
          'post_type'   => 'page' // Custom Post Type
        );

        $post_id = wp_insert_post( $post ); // Use the WordPress default wp_insert_post function
        $metaDescription = 'Meta Descrption';

        update_post_meta($post_id, 'trackId', $siteId, true); // Set the App ID with the $siteId
        update_post_meta($post_id, 'trackName', $post_title, true); // Set the App Name with the $siteName
        update_post_meta($post_id, 'metaDescription', $metaDescription, true);
      };
    };
    return; // End the function
    restore_current_blog();
  }
  add_action('wpmu_new_blog', 'wpmu_app_sites', 10, 2);
?>
  • your function is hooked to run on blog creation, and within your function you create blogs? – Milo Dec 28 '16 at 17:57
  • @milo - Yes, I hook into a blog creation b/c it creates a 'main subsite', and that main subsite creates additional (child) subsites in the for loop. The 'main subsite' is being created w/o an issue, it's just the child subsites are now adding content to the main site. – Roc Dec 28 '16 at 18:00
  • what prevents your code from executing every time a subsite is created? if your function creates 3 subsites, your code runs again for each of the subsites as it does for the main site. – Milo Dec 28 '16 at 18:04
  • You need to verify the return results from wpmu_create_blog() before continuing. It could be an error... which is my guess is what is happening. – user42826 Dec 28 '16 at 20:59

Your Answer

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

Browse other questions tagged or ask your own question.