1、创建 WordPress 数据库表
在 Supabase
中创建与 WordPress
相关的基本表。以下是一个简单的示例,从几个核心表开始:
创建 wp_posts 表
CREATE TABLE wp_posts (
ID SERIAL PRIMARY KEY,
post_author BIGINT NOT NULL,
post_date TIMESTAMP NOT NULL,
post_date_gmt TIMESTAMP NOT NULL,
post_content TEXT NOT NULL,
post_title TEXT NOT NULL,
post_excerpt TEXT,
post_status VARCHAR(20) NOT NULL,
comment_status VARCHAR(20) NOT NULL,
ping_status VARCHAR(20) NOT NULL,
post_password VARCHAR(20),
post_name VARCHAR(200),
to_ping TEXT,
pinged TEXT,
post_modified TIMESTAMP NOT NULL,
post_modified_gmt TIMESTAMP NOT NULL,
post_content_filtered TEXT,
post_parent BIGINT,
guid TEXT NOT NULL,
menu_order INTEGER NOT NULL DEFAULT 0,
post_type VARCHAR(20) NOT NULL,
post_mime_type VARCHAR(100),
comment_count BIGINT NOT NULL DEFAULT 0
);
创建 wp_terms 表
CREATE TABLE wp_terms (
term_id SERIAL PRIMARY KEY,
name VARCHAR(200) NOT NULL,
slug VARCHAR(200),
term_group BIGINT DEFAULT 0
);
创建 wp_term_taxonomy 表
CREATE TABLE wp_term_taxonomy (
term_taxonomy_id SERIAL PRIMARY KEY,
term_id BIGINT NOT NULL,
taxonomy VARCHAR(32) NOT NULL,
description TEXT,
parent BIGINT DEFAULT 0,
count BIGINT DEFAULT 0,
FOREIGN KEY (term_id) REFERENCES wp_terms(term_id)
);
创建 wp_term_relationships 表
CREATE TABLE wp_term_relationships (
object_id BIGINT NOT NULL,
term_taxonomy_id BIGINT NOT NULL,
PRIMARY KEY (object_id, term_taxonomy_id),
FOREIGN KEY (object_id) REFERENCES wp_posts(ID),
FOREIGN KEY (term_taxonomy_id) REFERENCES wp_term_taxonomy(term_taxonomy_id)
);
2、插入文章数据
插入一条文章数据,假设我们已经有了一个作者(ID 为 1)和一个分类(ID 为 1):
插入一条文章到 wp_posts
INSERT INTO wp_posts (
post_author,
post_date,
post_date_gmt,
post_content,
post_title,
post_status,
comment_status,
ping_status,
post_modified,
post_modified_gmt,
post_type
) VALUES (
1, -- 作者 ID
'2024-07-31 10:00:00',
'2024-07-31 10:00:00',
'这是文章的内容。',
'示例文章标题',
'publish',
'open',
'closed',
'2024-07-31 10:00:00',
'2024-07-31 10:00:00',
'post'
) RETURNING ID;
这条 SQL 插入语句会返回新插入文章的 ID。
3、处理分类和标签
在 WordPress 中,文章的分类和标签可以通过 wp_term_relationships 与 wp_terms 和 wp_term_taxonomy 挂钩。
插入分类和标签
INSERT INTO wp_terms (name, slug) VALUES ('分类名称', '分类-slug') RETURNING term_id;
INSERT INTO wp_terms (name, slug) VALUES ('标签名称', '标签-slug') RETURNING term_id;
假设上述插入返回的 term_id 分别是 1(分类)和 2(标签)。
插入到 wp_term_taxonomy
INSERT INTO wp_term_taxonomy (term_id, taxonomy, description) VALUES (1, 'category', '分类描述');
INSERT INTO wp_term_taxonomy (term_id, taxonomy, description) VALUES (2, 'post_tag', '标签描述');
假设这两条插入分别返回的 term_taxonomy_id 是 1(分类)和 2(标签)。
关联文章与分类和标签
INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (%POST_ID%, 1); -- 关联分类
INSERT INTO wp_term_relationships (object_id, term_taxonomy_id) VALUES (%POST_ID%, 2); -- 关联标签
这里的%POST_ID%
是在插入文章时返回的 ID
。
4、 使用 Supabase 客户端插入数据
同样,你可以使用 Supabase 客户端库来执行这些插入操作:
const { createClient } = require('@supabase/supabase-js');
const supabaseUrl = 'https://your-supabase-url.supabase.co';
const supabaseKey = 'your-supabase-key';
const supabase = createClient(supabaseUrl, supabaseKey);
async function insertWordPressData() {
// 插入文章
const { data: postData, error: postError } = await supabase
.from('wp_posts')
.insert([
{
post_author: 1,
post_date: '2024-07-31 10:00:00',
post_date_gmt: '2024-07-31 10:00:00',
post_content: '这是文章的内容。',
post_title: '示例文章标题',
post_status: 'publish',
comment_status: 'open',
ping_status: 'closed',
post_modified: '2024-07-31 10:00:00',
post_modified_gmt: '2024-07-31 10:00:00',
post_type: 'post'
}
])
.select('ID') // 获取生成的 ID
.single();
if (postError) {
console.error('插入文章错误:', postError);
} else {
const postId = postData.ID;
// 插入分类(假设返回的新分类 ID 为 1)
const { data: termData, error: termError } = await supabase
.from('wp_terms')
.insert([{ name: '分类名称', slug: '分类-slug' }]);
const categoryId = termData[0].term_id;
// 插入分类的 taxonomy
await supabase
.from('wp_term_taxonomy')
.insert([{ term_id: categoryId, taxonomy: 'category', description: '分类描述' }]);
// 关联文章和分类
await supabase
.from('wp_term_relationships')
.insert([{ object_id: postId, term_taxonomy_id: categoryId }]);
console.log('数据插入成功!');
}
}
insertWordPressData();