我有Wordpress Multisite安装。有5个子站点。我想发布和编辑子站点No中的所有帖子。 1.即我想在子网站no上发布帖子。 1,然后单击“发布”按钮,我希望将此帖子发布到我的网络的每个子站点上。我还想编辑子站点编号上的帖子。 1,并在点击按钮更新后,希望在整个子网站网络上更新此帖子。
我能够编写脚本,使我能够在每个子站点上创建新帖子,但是当我更新帖子时,脚本会创建新帖子而不是编辑帖子,这是主要问题。我已经尝试过另一个钩子,例如“ edit_post”或“ post_updated”,但是它没有用。我也无法使用WP REST API来达到目的。
有人有创意吗?
在此先感谢您的帮助。
function kush_update_posts_on_subsites( $post_id, $post, $update ) {
// This function can be called only once - infinite loop secured.
// @link https://stackoverflow.com/questions/3695663/functions-that-can-be-called-only-once
static $foo_called = false;
if ($foo_called) return;
$foo_called = true;
// Define post params.
$id = null; // Post id must be null to create new post. When it's set $post_id it can edit post but it can't create new posts, ie.: $id = $post_id;
$post_name = $post->post_name;
$post_title = $post->post_title;
$post_status = 'publish';
$post_type = 'kush_link';
// Get number of all subsites in Multisite Network.
$blog_count = get_blog_count();
foreach( get_sites( array( 'number' => $blog_count ) ) as $subsite ) {
// Get subsite id.
$subsite_id = get_object_vars($subsite)["blog_id"];
// Skipping subsite 1 to avoid creating an empty draft.
if ($subsite_id == 1) {
continue;
}
// Switch to the appropriate subsite.
// @link https://codex.wordpress.org/Function_Reference/switch_to_blog
switch_to_blog( $subsite_id );
// Prepare post params.
// https://developer.wordpress.org/reference/functions/wp_update_post/
$my_post = array(
'ID' => $id,
'post_name' => $post_name,
'post_title' => $post_title,
'post_status' => $post_status,
'post_type' => $post_type,
);
// Insert the post into the database.
wp_update_post( $my_post );
// Return to current subsite ie. subsite = 1.
restore_current_blog();
}
}
// @link https://codex.wordpress.org/Plugin_API/Action_Reference/save_post
add_action( 'save_post', 'kush_update_posts_on_subsites', 10, 3 );