开发者

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

开发者 https://www.devze.com 2023-02-17 03:14 出处:网络
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.

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 pos开发者_如何学Pythont 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.


I got the answer for this... The culprit is $blog_id, the moment I changed the variable name to $new_blog_id, it started working. Thanks


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();
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号