wordpress-content

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

WordPress Content

WordPress 内容管理

Create, update, and manage WordPress content — posts, pages, media, categories, tags, and menus. Produces live content on the site via WP-CLI or the REST API.
创建、更新和管理WordPress内容——文章、页面、媒体、分类、标签和菜单。通过WP-CLI或REST API在站点上生成实时内容。

Prerequisites

前提条件

  • Working WP-CLI SSH connection or REST API credentials (use wordpress-setup skill)
  • Site config from
    wordpress.config.json
    or
    wp-cli.yml
  • 可用的WP-CLI SSH连接或REST API凭证(使用wordpress-setup技能)
  • 来自
    wordpress.config.json
    wp-cli.yml
    的站点配置

Workflow

工作流程

Step 1: Determine the Operation

步骤1:确定操作类型

TaskBest Method
Create/edit single post or pageWP-CLI
wp post create/update
Bulk create postsWP-CLI loop or REST API batch
Upload images/mediaWP-CLI
wp media import
Manage categories/tagsWP-CLI
wp term
Update navigation menusWP-CLI
wp menu
Scheduled postsWP-CLI with
--post_date
Complex HTML contentWrite to temp file, pass to WP-CLI
任务最佳方法
创建/编辑单篇文章或页面WP-CLI
wp post create/update
批量创建文章WP-CLI循环或REST API批量操作
上传图片/媒体WP-CLI
wp media import
管理分类/标签WP-CLI
wp term
更新导航菜单WP-CLI
wp menu
定时发布文章
--post_date
参数的WP-CLI
复杂HTML内容写入临时文件,传递给WP-CLI

Step 2: Create Content

步骤2:创建内容

Blog Posts

博客文章

bash
undefined
bash
undefined

Simple post

简单文章

wp @site post create
--post_type=post
--post_title="My New Blog Post"
--post_content="<p>Post content here.</p>"
--post_status=draft
--post_category=3,5
wp @site post create
--post_type=post
--post_title="My New Blog Post"
--post_content="<p>Post content here.</p>"
--post_status=draft
--post_category=3,5

Post from HTML file (better for long content)

从HTML文件创建文章(适合长内容)

wp @site post create ./post-content.html
--post_type=post
--post_title="My New Blog Post"
--post_status=draft
--post_excerpt="A brief summary of the post."
--post_category=3,5
--tags_input="tag1,tag2"

**Post statuses**: `draft`, `publish`, `pending`, `future` (use with `--post_date`)
wp @site post create ./post-content.html
--post_type=post
--post_title="My New Blog Post"
--post_status=draft
--post_excerpt="A brief summary of the post."
--post_category=3,5
--tags_input="tag1,tag2"

**文章状态**:`draft`(草稿)、`publish`(已发布)、`pending`(待审核)、`future`(定时发布,需配合`--post_date`参数)

Pages

页面

bash
wp @site post create \
  --post_type=page \
  --post_title="About Us" \
  --post_content="<h2>Our Story</h2><p>Content here...</p>" \
  --post_status=publish \
  --post_parent=0 \
  --menu_order=10
bash
wp @site post create \
  --post_type=page \
  --post_title="About Us" \
  --post_content="<h2>Our Story</h2><p>Content here...</p>" \
  --post_status=publish \
  --post_parent=0 \
  --menu_order=10

Scheduled Posts

定时发布文章

bash
wp @site post create \
  --post_type=post \
  --post_title="Scheduled Post" \
  --post_content="<p>This goes live tomorrow.</p>" \
  --post_status=future \
  --post_date="2026-02-23 09:00:00"
bash
wp @site post create \
  --post_type=post \
  --post_title="Scheduled Post" \
  --post_content="<p>This goes live tomorrow.</p>" \
  --post_status=future \
  --post_date="2026-02-23 09:00:00"

Step 3: Upload Media

步骤3:上传媒体

bash
undefined
bash
undefined

Upload from URL

从URL上传

wp @site media import "https://example.com/image.jpg"
--title="Product Photo"
--alt="Product front view"
--caption="Our latest product"
wp @site media import "https://example.com/image.jpg"
--title="Product Photo"
--alt="Product front view"
--caption="Our latest product"

Upload from local file (requires SCP first for remote sites)

从本地文件上传(远程站点需先通过SCP上传文件)

scp ./image.jpg user@host:/tmp/image.jpg wp @site media import /tmp/image.jpg --title="Local Upload"

**Set featured image on a post**:

```bash
scp ./image.jpg user@host:/tmp/image.jpg wp @site media import /tmp/image.jpg --title="Local Upload"

**为文章设置特色图片**:

```bash

Get the attachment ID from the import output, then:

从上传输出中获取附件ID,然后执行:

wp @site post meta update {post_id} _thumbnail_id {attachment_id}
undefined
wp @site post meta update {post_id} _thumbnail_id {attachment_id}
undefined

Step 4: Manage Taxonomy

步骤4:管理分类法

Categories

分类

bash
undefined
bash
undefined

List categories

列出所有分类

wp @site term list category --fields=term_id,name,slug,count
wp @site term list category --fields=term_id,name,slug,count

Create category

创建分类

wp @site term create category "News" --slug=news --description="Company news and updates"
wp @site term create category "News" --slug=news --description="Company news and updates"

Create child category

创建子分类

wp @site term create category "Product News" --slug=product-news --parent=5
wp @site term create category "Product News" --slug=product-news --parent=5

Assign category to post

为文章分配分类

wp @site post term add {post_id} category news
undefined
wp @site post term add {post_id} category news
undefined

Tags

标签

bash
undefined
bash
undefined

Add tags during post creation

创建文章时添加标签

wp @site post create --post_title="..." --tags_input="seo,marketing,tips"
wp @site post create --post_title="..." --tags_input="seo,marketing,tips"

Add tags to existing post

为已有文章添加标签

wp @site post term add {post_id} post_tag seo marketing tips
undefined
wp @site post term add {post_id} post_tag seo marketing tips
undefined

Step 5: Manage Menus

步骤5:管理菜单

bash
undefined
bash
undefined

List menus

列出所有菜单

wp @site menu list --fields=term_id,name,slug,count
wp @site menu list --fields=term_id,name,slug,count

List items in a menu

列出菜单中的项目

wp @site menu item list main-menu --fields=db_id,type,title,link
wp @site menu item list main-menu --fields=db_id,type,title,link

Add page to menu

向菜单添加页面

wp @site menu item add-post main-menu {page_id} --title="About Us"
wp @site menu item add-post main-menu {page_id} --title="About Us"

Add custom link

向菜单添加自定义链接

wp @site menu item add-custom main-menu "Contact" "https://example.com/contact/"
wp @site menu item add-custom main-menu "Contact" "https://example.com/contact/"

Reorder (set position)

重新排序(设置位置)

wp @site menu item update {item_id} --position=3
undefined
wp @site menu item update {item_id} --position=3
undefined

Step 6: Update Existing Content

步骤6:更新已有内容

bash
undefined
bash
undefined

Update post title and content

更新文章标题和内容

wp @site post update {post_id}
--post_title="Updated Title"
--post_content="<p>New content.</p>"
wp @site post update {post_id}
--post_title="Updated Title"
--post_content="<p>New content.</p>"

Update from file

从文件更新内容

wp @site post update {post_id} ./updated-content.html
wp @site post update {post_id} ./updated-content.html

Bulk update status

批量更新状态

wp @site post list --post_type=post --post_status=draft --field=ID |
xargs -I {} wp @site post update {} --post_status=publish
undefined
wp @site post list --post_type=post --post_status=draft --field=ID |
xargs -I {} wp @site post update {} --post_status=publish
undefined

Step 7: Verify

步骤7:验证结果

bash
undefined
bash
undefined

Check the post

查看文章信息

wp @site post get {post_id} --fields=ID,post_title,post_status,guid
wp @site post get {post_id} --fields=ID,post_title,post_status,guid

Get the live URL

获取文章的实时URL

wp @site post get {post_id} --field=guid
wp @site post get {post_id} --field=guid

List recent posts

列出最新文章

wp @site post list --post_type=post --posts_per_page=5 --fields=ID,post_title,post_status,post_date

Provide the admin URL and live URL:
- Admin: `https://example.com/wp-admin/post.php?post={id}&action=edit`
- Live: `https://example.com/{slug}/`

---
wp @site post list --post_type=post --posts_per_page=5 --fields=ID,post_title,post_status,post_date

提供后台管理URL和实时访问URL:
- 后台:`https://example.com/wp-admin/post.php?post={id}&action=edit`
- 实时:`https://example.com/{slug}/`

---

Critical Patterns

关键模式

HTML Content in WP-CLI

WP-CLI中的HTML内容

For anything beyond a sentence, write HTML to a temp file and pass it:
bash
cat > /tmp/post-content.html << 'EOF'
<h2>Section Heading</h2>
<p>Paragraph content with <strong>bold</strong> and <a href="/link">links</a>.</p>
<ul>
  <li>List item one</li>
  <li>List item two</li>
</ul>
EOF

wp @site post create /tmp/post-content.html --post_title="My Post" --post_status=draft
Shell quoting in
--post_content
is fragile for complex HTML.
对于超过一句话的内容,建议将HTML写入临时文件后再传递给WP-CLI:
bash
cat > /tmp/post-content.html << 'EOF'
<h2>Section Heading</h2>
<p>Paragraph content with <strong>bold</strong> and <a href="/link">links</a>.</p>
<ul>
  <li>List item one</li>
  <li>List item two</li>
</ul>
EOF

wp @site post create /tmp/post-content.html --post_title="My Post" --post_status=draft
--post_content
参数中使用Shell引号处理复杂HTML时容易出错。

Bulk Operations

批量操作

For creating many posts, use a loop with verification:
bash
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
Always create as
draft
first, review, then bulk-publish.
创建多篇文章时,建议使用循环并配合验证:
bash
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
(草稿)状态创建,审核后再批量发布。

ACF Custom Fields

ACF自定义字段

If Advanced Custom Fields is installed:
bash
undefined
如果已安装Advanced Custom Fields(高级自定义字段):
bash
undefined

Set ACF field

设置ACF字段

wp @site post meta update {post_id} field_name "value"
wp @site post meta update {post_id} field_name "value"

Get ACF field

获取ACF字段值

wp @site post meta get {post_id} field_name

ACF stores fields with both the field value and a reference key (`_field_name` → `field_abc123`).
wp @site post meta get {post_id} field_name

ACF会同时存储字段值和引用键(`_field_name` → `field_abc123`)。

REST API Alternative

REST API替代方案

When WP-CLI isn't available, use the REST API. See
references/rest-api-endpoints.md
for patterns.

当无法使用WP-CLI时,可以使用REST API。请参考
references/rest-api-endpoints.md
中的使用模式。

Reference Files

参考文件

  • references/rest-api-endpoints.md
    — REST API endpoints with authentication examples
  • references/wp-cli-content.md
    — WP-CLI content management command reference
  • references/rest-api-endpoints.md
    — 包含认证示例的REST API端点文档
  • references/wp-cli-content.md
    — WP-CLI内容管理命令参考文档