Yoast SEO 和 SEOPress 都使用标准 WordPress post meta 存储 SEO 数据,因此可以通过 WP-CLI 和 REST API 进行管理。
Meta Keys 对照表
| 用途 | Yoast SEO | SEOPress |
|---|---|---|
| SEO 标题 | _yoast_wpseo_title | _seopress_titles_title |
| Meta 描述 | _yoast_wpseo_metadesc | _seopress_titles_desc |
| 焦点关键词 | _yoast_wpseo_focuskw | _seopress_analysis_target_kw |
| 规范 URL | _yoast_wpseo_canonical | _seopress_robots_canonical |
Yoast SEO
WP-CLI(直接可用)
bash
1234567891011
# 设置 SEO 标题
wp post meta update <post_id> _yoast_wpseo_title "你的SEO标题"
# 设置 Meta 描述
wp post meta update <post_id> _yoast_wpseo_metadesc "你的Meta描述"
# 设置焦点关键词
wp post meta update <post_id> _yoast_wpseo_focuskw "关键词"
# 查看 SEO Meta
wp post meta get <post_id> _yoast_wpseo_titleREST API(需注册字段)
默认情况下 REST API 不支持直接更新 Yoast meta(以 _ 开头的字段被视为私有)。
在 functions.php 或插件中添加:
php
12345678910111213141516171819
add_action('init', function() {
$yoast_fields = [
'_yoast_wpseo_title',
'_yoast_wpseo_metadesc',
'_yoast_wpseo_focuskw',
'_yoast_wpseo_canonical',
];
foreach ($yoast_fields as $field) {
register_post_meta('post', $field, [
'show_in_rest' => true,
'single' => true,
'type' => 'string',
'auth_callback' => function() {
return current_user_can('edit_posts');
}
]);
}
});然后使用 REST API:
bash
123456789
curl -X POST "https://your-site.com/wp-json/wp/v2/posts/<post_id>" \
-H "Authorization: Basic <base64_credentials>" \
-H "Content-Type: application/json" \
-d '{
"meta": {
"_yoast_wpseo_title": "SEO标题",
"_yoast_wpseo_metadesc": "Meta描述"
}
}'SEOPress
WP-CLI(直接可用)
bash
12345678
# 设置 SEO 标题
wp post meta update <post_id> _seopress_titles_title "你的SEO标题"
# 设置 Meta 描述
wp post meta update <post_id> _seopress_titles_desc "你的Meta描述"
# 查看 SEO Meta
wp post meta get <post_id> _seopress_titles_titleREST API(直接可用)
SEOPress 已内置 REST API 支持,无需额外配置:
bash
123456789
curl -X POST "https://your-site.com/wp-json/wp/v2/posts/<post_id>" \
-H "Authorization: Basic <base64_credentials>" \
-H "Content-Type: application/json" \
-d '{
"meta": {
"_seopress_titles_title": "SEO标题",
"_seopress_titles_desc": "Meta描述"
}
}'批量操作示例
bash
12345
# 批量给所有文章设置描述(Yoast)
wp post list --post_type=post --format=ids | xargs -I {} wp post meta update {} _yoast_wpseo_metadesc "默认描述"
# 批量给所有文章设置描述(SEOPress)
wp post list --post_type=post --format=ids | xargs -I {} wp post meta update {} _seopress_titles_desc "默认描述"对比总结
| 特性 | Yoast WP-CLI | Yoast REST API | SEOPress WP-CLI | SEOPress REST API |
|---|---|---|---|---|
| 开箱即用 | ✅ | ❌ 需注册字段 | ✅ | ✅ |
| 远程调用 | ❌ 需 SSH | ✅ | ❌ 需 SSH | ✅ |
| 适合自动化 | ✅ | ✅ | ✅ | ✅ |
结论:
- Yoast:WP-CLI 直接可用,REST API 需要额外注册字段
- SEOPress:WP-CLI 和 REST API 都开箱即用,更适合自动化场景