简体   繁体   中英

How to replace all pages in one WordPress multisite site from another site (on same network)

I have a WordPress multisite installation, latest version.

I have one site that acts as a template for all other sites. No problem when a new site is added as all pages are copied across (WPMU Dev Copier plugin).

However, for older sites already created I would like to replace the content pages for all sites, from the "template" site. This can include newly created sites as well as older ones to keep it simpler.

Is there a simple way using PHP that I can loop through all sites other than the template site, and if the post_name (slug) of a page is in an array, copy the content of that page from the template site to each site? I do not really want to delete the page and re-create it, but would like to replace the post_content.

Looking for any hidden functions that can return pages for a site, for example, unless there exists a plugin that I've overlooked. Otherwise I guess I'm coding ;)

Thank you.

So these is the raw version (not yet optimised), that copies from current blog (site) to all other blogs (sites) except #1. Where post_name exists, content is copied, otherwise assumed a new page, and one is created. New page ownership given to currently logged in user.

Needs to be changed to handle large installations, as does all in one go at the moment, with some paging, etc.

Suitable for my requirements, could probably be played around with for other situations. So shared in case of use to others.

$id = get_current_blog_id(); $sql = "SELECT * FROM ".$wpdb->base_prefix."blogs WHERE blog_id != %d AND blog_id > 1"; $blogs = $wpdb->get_results($wpdb->prepare($sql, $id)); foreach ($blogs as $blog): echo $blog->path.' ('.$blog->blog_id.')<br />'; $blog_id = ($id == 1) ? '' : $id.'_'; $sql = "SELECT * FROM ".$wpdb->base_prefix.$blog_id."posts WHERE post_type='page' and post_status='publish'"; $pages = $wpdb->get_results($sql); foreach ($pages as $page): $sql = "SELECT ID FROM ".$wpdb->base_prefix.$blog->blog_id."_posts WHERE post_name = %s AND post_status = 'publish'"; $target = $wpdb->get_row($wpdb->prepare($sql, $page->post_name)); if ($target): $sql = "UPDATE ".$wpdb->base_prefix.$blog->blog_id."_posts SET post_content = %s WHERE post_type='page' AND post_status = 'publish' AND post_name = %s"; $wpdb->query($wpdb->prepare($sql, $page->post_content, $page->post_name)); else: $post_author = $current_user->ID; $post_content = $page->post_content; $post_title = $page->post_title; $comment_status = 'closed'; $post_name = $page->post_name; $post_type = 'page'; $insert = $wpdb->prepare( "( %d, %s, %s, %s, %s, %s)", $post_author, $post_content, $post_title, $comment_status, $post_name, $post_type ); $wpdb->query( "INSERT INTO ".$wpdb->base_prefix.$blog->blog_id."_posts ( post_author, post_content, post_title, comment_status, post_name, post_type ) VALUES " . $insert );
endif; endforeach; endforeach;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM