• I’ve a wordpress multisite setup in xampp server. I want to post using wp_insert_post() function from a external php page to a specific site of multisite. SO far, I’ve tried the following code, but it creates post to main site, not specific site of multisite.

    <?php
    
    // Load WordPress
    require_once("../../../wp-load.php"); 
    require_once ABSPATH . '/wp-admin/includes/taxonomy.php';
    
    $my_post = array(
    
      'post_title' => 'The post title via wp_insert_post Hello World!',
    
      'post_content' => '<h4>Place all your body content for the post in this line.</h4>
      						[wbcr_snippet id="106" url="flebaixgtscj"]	',
      'post_status'   => 'publish',
      'post_author'   => $user_id,
      'post_category' => array(9),
      'post_type' => 'post', // defaults to "post". Can be set to CPTs.
      'comment_status' =>'closed'
    );
     
    // Insert the post into the database
    
    $id=wp_insert_post( $my_post );
    
    echo "post id: ".$id."<br>";
    
    ?>

    This custom_post.php file is located at the wp theme folder of specific site.

    How to do this ? please help.

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Use switch_to_blog() to target a specific subsite when functions like wp_insert_post() are called.

    FYI, requiring wp-load.php to invoke the WP environment is not portable to some sites. If it works for your site, fine. But don’t expect to work on everyone’s site. /wp-content/ can be moved so that relative references don’t always work. There are limited valid ways to invoke WP from an externally requested file. Requesting through /wp-admin/admin-post.php is one option. Ajax and page templates are the others.

Viewing 1 replies (of 1 total)
  • The topic ‘Creating post from external php file to a specific site of multisite’ is closed to new replies.