其他
用 WP-CLI 或 REST API 管理…
当需要通过命令行或 REST API 在 WordPress 站点上创建、更新、发布、排期或批量管理文章、页面、媒体、分类、标签和导航菜单时使用。
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
GDI C 35/100
进化评分(GDI)
综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」
内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 38/100 ×0.15
适用场景
用 WP-CLI 或 REST API 特定
解决思路
- 先判断操作类型:单篇创建/编辑用 WP-CLI `wp post create/update`;批量创建用 WP-CLI 循环或 REST API 批量;上传媒体用 `wp media import`;管理分类/标签用 `wp term`;更新导航菜单用 `wp menu`;定时发布用带 `--post_date` 的 WP-CLI;复杂 HTML 内容写入临时文件再传给 WP-CLI;无 SSH 访问时用带 Application Password 的 REST API。
- 创建简单文章:`wp @site post create --post_type=post --post_title="..." --post_content="<p>...</p>" --post_status=draft --post_category=3,5`。
- 从 HTML 文件创建长文章:`wp @site post create ./post-content.html --post_type=post --post_title="..." --post_status=draft --post_excerpt="..." --post_category=3,5 --tags_input="tag1,tag2"`。
- 文章状态可用:`draft`、`publish`、`pending`、`future`(配合 `--post_date`)。
- 创建页面:`wp @site post create --post_type=page --post_title="About Us" --post_content="..." --post_status=publish --post_parent=0 --menu_order=10`。
- 定时发布:`wp @site post create --post_type=post --post_title="..." --post_content="..." --post_status=future --post_date="2026-02-23 09:00:00"`。
- 从 URL 上传媒体:`wp @site media import "https://example.com/image.jpg" --title="..." --alt="..." --caption="..."`。
- 从本地文件上传媒体(远程站点需先 SCP):`scp ./image.jpg user@host:/tmp/image.jpg` 然后 `wp @site media import /tmp/image.jpg --title="Local Upload"`。
- 导入媒体并一步设为特色图片:`wp @site media import "https://example.com/hero.jpg" --title="Hero" --featured_image --post_id={id}`。
- 列出媒体:`wp @site post list --post_type=attachment --fields=ID,post_title,guid`。
- 重新生成缩略图:`wp @site media regenerate --yes`。
- 为文章设置特色图片:从导入输出获取附件 ID,然后 `wp @site post meta update {post_id} _thumbnail_id {attachment_id}`。
- 列出分类:`wp @site term list category --fields=term_id,name,slug,count`。
- 创建分类:`wp @site term create category "News" --slug=news --description="..."`。
- 创建子分类:`wp @site term create category "Product News" --slug=product-news --parent=5`。
- 更新分类:`wp @site term update category {term_id} --name="Updated Name"`。
- 为文章分配分类:`wp @site post term add {post_id} category news`。
- 列出标签:`wp @site term list post_tag --fields=term_id,name,slug,count`。
- 创建标签:`wp @site term create post_tag "new-tag"`。
- 创建文章时添加标签:`wp @site post create --post_title="..." --tags_input="seo,marketing,tips"`。
- 为已有文章添加标签:`wp @site post term add {post_id} post_tag seo marketing tips`。
- 列出菜单:`wp @site menu list --fields=term_id,name,slug,count`。
- 列出菜单项:`wp @site menu item list main-menu --fields=db_id,type,title,link,position`。
- 向菜单添加页面:`wp @site menu item add-post main-menu {page_id} --title="About Us"`。
- 向菜单添加自定义链接:`wp @site menu item add-custom main-menu "Contact" "https://example.com/contact/"`。
- 向菜单添加分类归档:`wp @site menu item add-term main-menu category {term_id}`。
- 重排菜单项(设置位置):`wp @site menu item update {item_id} --position=3`。
- 删除菜单项:`wp @site menu item delete {item_id}`。
- 更新文章标题和内容:`wp @site post update {post_id} --post_title="Updated Title" --post_content="<p>New content.</p>"`。
- 从文件更新文章:`wp @site post update {post_id} ./updated-content.html`。
- 搜索文章:`wp @site post list --s="search term" --fields=ID,post_title`。
- 批量更新状态:`wp @site post list --post_type=post --post_status=draft --field=ID | xargs -I {} wp @site post update {} --post_status=publish`。
- 删除文章(移入回收站):`wp @site post delete {post_id}`;永久删除:`wp @site post delete {post_id} --force`。
- 列出文章所有 meta:`wp @site post meta list {post_id} --fields=meta_key,meta_value`。
- 获取指定 meta:`wp @site post meta get {post_id} meta_key`。
- 设置 meta:`wp @site post meta update {post_id} meta_key "meta_value"`。
- 添加 meta(允许重复):`wp @site post meta add {post_id} meta_key "meta_value"`。
- 删除 meta:`wp @site post meta delete {post_id} meta_key`。
- 注意 ACF 会同时存储字段值和引用键(`_field_name` -> `field_abc123`)。
- 搜索替换前先干跑:`wp @site search-replace "old text" "new text" --dry-run`。
- 执行搜索替换:`wp @site search-replace "old text" "new text" --precise`。
- 限定到特定表:`wp @site search-replace "old" "new" wp_posts --precise`。
- 限定到特定列:`wp @site search-replace "old" "new" wp_posts post_content --precise`。
- 导出全部内容:`wp @site export --dir=/tmp/`;导出特定文章类型:`wp @site export --post_type=post --dir=/tmp/`。
- 导入:`wp @site import /path/to/file.xml --authors=mapping.csv`。
- 验证文章:`wp @site post get {post_id} --fields=ID,post_title,post_status,guid`。
- 获取线上 URL:`wp @site post get {post_id} --field=guid`。
- 列出最近文章:`wp @site post list --post_type=post --posts_per_page=5 --fields=ID,post_title,post_status,post_date`。
- 提供后台 URL `https://example.com/wp-admin/post.php?post={id}&action=edit` 和线上 URL `https://example.com/{slug}/`。
- REST API 认证:`AUTH=$(echo -n "username:xxxx xxxx xxxx xxxx xxxx xxxx" | base64)`,然后在请求头加 `Authorization: Basic $AUTH` 和 `Content-Type: application/json`。
- REST API 端点:文章 `/wp-json/wp/v2/posts`、页面 `/wp-json/wp/v2/pages`、媒体 `/wp-json/wp/v2/media`、分类 `/wp-json/wp/v2/categories`、标签 `/wp-json/wp/v2/tags`,均支持 GET/POST/PUT/DELETE。
- 通过 REST 创建文章:`curl -s https://example.com/wp-json/wp/v2/posts -H "Authorization: Basic $AUTH" -H "Content-Type: application/json" -d '{"title":"...","content":"...","status":"draft","categories":[3,5],"tags":[10,12],"excerpt":"...","featured_media":456}' | jq '{id, link, status}'`。
- 通过 REST 创建页面:`curl -s https://example.com/wp-json/wp/v2/pages -H "Authorization: Basic $AUTH" -H "Content-Type: application/json" -d '{"title":"About Us","content":"...","status":"publish","parent":0}'`。
- 通过 REST 上传媒体:`curl -s https://example.com/wp-json/wp/v2/media -H "Authorization: Basic $AUTH" -H "Content-Disposition: attachment; filename=photo.jpg" -H "Content-Type: image/jpeg" --data-binary @photo.jpg | jq '{id, source_url}'`。
- 通过 REST 创建分类:`curl -s https://example.com/wp-json/wp/v2/categories -H "Authorization: Basic $AUTH" -H "Content-Type: application/json" -d '{"name":"News","slug":"news","description":"..."}'`。
- REST 查询参数:`per_page`(每页最多 100)、`page`(分页)、`search`(搜索词)、`status`(按状态过滤)、`categories`(按分类 ID 过滤)、`orderby`(排序字段)、`order`(排序方向)、`_fields`(限制返回字段)。
- 导航菜单的 REST API 支持有限:`/wp-json/wp/v2/navigation` 仅用于 FSE 主题的区块导航;经典菜单请用 WP-CLI。
- WP-CLI 中处理 HTML 内容:超过一句话的内容应写入临时文件再传入,例如用 heredoc 写入 `/tmp/post-content.html` 后 `wp @site post create /tmp/post-content.html --post_title="My Post" --post_status=draft`,因为 `--post_content` 的 shell 引号对复杂 HTML 很脆弱。
- 批量操作:用循环加验证,例如 `while IFS=, read -r title slug content_file category; do wp @site post create "$content_file" --post_type=post --post_title="$title" --post_name="$slug" --post_category="$category" --post_status=draft; sleep 0.5; done < posts.csv`。
- 批量操作始终先创建为 `draft`,审核后再批量发布。
适用边界 · 注意事项
- 没有可用的 WP-CLI SSH 连接或 REST API 凭据时无法执行(需先用 wordpress-setup 技能配置)。
- 经典导航菜单无法通过 REST API 管理,必须使用 WP-CLI。
- 复杂 HTML 内容不要直接放在 `--post_content` 参数里,shell 引号容易出错,应写入临时文件。