首页 > 代码

同一台服务器里的两个wordpress数据库文章内容叠加复制

问题:同一台服务器里的两个wordpress数据库,我要把名为temp数据库里的wp_posts的所有文章 全部复制到名为cuwen数据库wp_posts里,cuwen里已经有文章,复制过来的文章当做新加,不要删除或覆盖老数据文章。
使用下面的 SQL 脚本,将一台服务器上的 temp 数据库中的 wp_posts 表内容,完整复制到已有内容的 cuwen.wp_posts 表中,并确保所有复制过来的文章在 cuwen 数据库中是作为“新文章”插入(不会覆盖原有文章):
安全复制文章 SQL 语句(适用于 WordPress 标准结构)


INSERT INTO cuwen.wp_posts (
    post_author, post_date, post_date_gmt,
    post_content, post_title, post_excerpt,
    post_status, comment_status, ping_status,
    post_password, post_name, to_ping, pinged,
    post_modified, post_modified_gmt,
    post_content_filtered, post_parent,
    guid, menu_order, post_type, post_mime_type, comment_count
)
SELECT 
    post_author, post_date, post_date_gmt,
    post_content, post_title, post_excerpt,
    post_status, comment_status, ping_status,
    post_password, post_name, to_ping, pinged,
    post_modified, post_modified_gmt,
    post_content_filtered, 0 AS post_parent,  -- 置为0避免引用不存在的父文章
    guid, menu_order, post_type, post_mime_type, comment_count
FROM temp.wp_posts
WHERE post_type IN ('post', 'page');  -- 只复制文章和页面(如有其他类型可扩展)

说明:
1、ID 字段会自动增长(前提是 ID 为自增主键,标准 WordPress 表结构是这样)。
2、不复制 ID 字段,是为了让 MySQL 为 cuwen.wp_posts 自动分配新文章 ID,避免覆盖。
3、如果你使用了自定义文章类型或需要全部类型,去掉 WHERE post_type IN (…) 条件即可。
4、post_parent 设置为 0,防止引用原数据库中不存在的父文章。


  • 批量设置wordpress文章分类,把文章 ID 区间为 1256869 到 1257421批量设置分类为 ID =5
  • 将 cuwen.wp_posts 中 文章 ID 区间为 1256869 到 1257421(含) 的文章批量 […]

  • WordPress 如何在首页与内页用不同的菜单或header
  • 开发wordpress主题时,首页的header内容与内页的header内容希望不一样,这时就要用到WordP […]

  • wordpress有必要在body内加<body <?php body_class(); ?>吗?
  • 是 WordPress 主题开发中非常有用的一部分,建议保留。它会自动输出一些根据页面类型、用户状态、文章分类 […]

  • 推荐几个wordpress生成 整站 sitemap插件
  • 方法一:使用 WordPress 插件(推荐) 1. **使用 Rank Math SEO 插件(推荐)) 优 […]

  • 全局禁用 WordPress 查询用户列表
  • 全局禁用 WordPress 查询用户列表 方法一:用 pre_user_query 拦截并中断不必要的用户查 […]

  • wordpress_分页访问记录防刷统计功能(如一天同 IP 多次访问超页数则封 IP
  • 分页防刷统计与封锁机制的完整代码,可配合你已有的分页访问限制功能使用,实现更强的反采集防御: 功能目标 每个 […]