我有一个PHP程序,可以在支持网络的WordPress中创建一个站点/博客。 创建网站后,我想使用相同的PHP程序创建一个帖子。
如果我使用wp_insert_post()函数,它会在主站点/博客中创建帖子,而不是在我创建的新站点/博客中。 我也尝试在调用wp_insert_post()之前使用switch_to_blog() wp_insert_post()但没有运气。
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.
最满意答案
以下用作Must Use插件可以完成这项工作:
<?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(); }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
更多推荐
发布评论