在WP MU的一个博客中使用PHP创建帖子(Creating a post using PHP in one of the blogs in WP MU)

编程入门 行业动态 更新时间:2024-10-23 20:22:00
在WP MU的一个博客中使用PHP创建帖子(Creating a post using PHP in one of the blogs in WP MU)

我有一个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

更多推荐

本文发布于:2023-08-04 08:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1412478.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:帖子   博客   WP   MU   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!