Skip to content

文章接口

发布文章(投稿)

Method : POST

URL : http://test.com/wp-json/vtheme/v1/posts/create

Auth required : True

Body :

json
{
  "post_title": "我的第一篇文章",
  "post_content": "<p>这是文章内容...</p>",
  "post_category": 5,
  "featured_image_id": 123,
  "post_status": "private"
}

参数说明

  • post_title (必填): 文章标题
  • post_content (必填): 文章内容(支持HTML)
  • post_category (可选): 分类ID
  • featured_image_id (可选): 特色图片附件ID
  • post_status (可选): 文章状态,默认为 private(待审核),可选值:private, draft, pending

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "message": "文章发布成功,等待管理员审核",
  "post_id": 456
}

字段说明

  • success: 操作是否成功
  • message: 提示信息
  • post_id: 新创建的文章ID

Error Response

Code : 400 Bad Request

Content examples :

json
{
  "code": "invalid_data",
  "message": "标题和内容不能为空",
  "data": {
    "status": 400
  }
}
json
{
  "code": "invalid_category",
  "message": "分类不存在",
  "data": {
    "status": 400
  }
}
json
{
  "code": "invalid_featured_image",
  "message": "特色图片ID无效或不是图片",
  "data": {
    "status": 400
  }
}

Code : 401 Unauthorized

Content examples :

json
{
  "code": "unauthorized",
  "message": "用户未登录",
  "data": {
    "status": 401
  }
}
json
{
  "code": "unauthorized",
  "message": "没有启用投稿",
  "data": {
    "status": 401
  }
}

获取我的文章列表

Method : GET

URL : http://test.com/wp-json/vtheme/v1/posts

Auth required : True

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "posts": [
    {
      "id": 456,
      "title": "我的第一篇文章",
      "content": "<p>这是文章内容...</p>",
      "status": "private",
      "date": "2024-01-15 10:30:00",
      "categories": ["WordPress", "教程"]
    },
    {
      "id": 457,
      "title": "第二篇文章",
      "content": "<p>更多内容...</p>",
      "status": "publish",
      "date": "2024-01-16 14:20:00",
      "categories": ["主题开发"]
    }
  ]
}

字段说明

  • success: 操作是否成功
  • posts: 文章列表数组
    • id: 文章ID
    • title: 文章标题
    • content: 文章内容(HTML格式)
    • status: 文章状态
      • publish: 已发布
      • private: 私有(待审核)
      • draft: 草稿
      • pending: 待审核
    • date: 发布日期
    • categories: 分类名称数组

Error Response

Code : 401 Unauthorized

Content example :

json
{
  "code": "unauthorized",
  "message": "用户未登录",
  "data": {
    "status": 401
  }
}

更新文章

Method : POST

URL : http://test.com/wp-json/vtheme/v1/posts/{id}/update

Auth required : True

URL Parameters :

  • id (必填): 文章ID

Body :

json
{
  "post_title": "更新后的标题",
  "post_content": "<p>更新后的内容...</p>",
  "post_category": 5,
  "featured_image_id": 124
}

参数说明

  • post_title (必填): 文章标题
  • post_content (必填): 文章内容
  • post_category (可选): 分类ID
  • featured_image_id (可选): 特色图片ID,传 0 或空字符串可移除特色图片

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "message": "文章更新成功"
}

Error Response

Code : 400 Bad Request

Content examples :

json
{
  "code": "invalid_data",
  "message": "标题和内容不能为空",
  "data": {
    "status": 400
  }
}
json
{
  "code": "invalid_category",
  "message": "分类不存在",
  "data": {
    "status": 400
  }
}
json
{
  "code": "invalid_featured_image",
  "message": "特色图片ID无效或不是图片",
  "data": {
    "status": 400
  }
}

Code : 401 Unauthorized

Content examples :

json
{
  "code": "unauthorized",
  "message": "用户未登录",
  "data": {
    "status": 401
  }
}
json
{
  "code": "unauthorized",
  "message": "没有启用投稿",
  "data": {
    "status": 401
  }
}

Code : 403 Forbidden

Content example :

json
{
  "code": "forbidden",
  "message": "没有权限编辑此文章",
  "data": {
    "status": 403
  }
}

说明:只有文章作者或管理员可以编辑文章

删除文章

Method : DELETE

URL : http://test.com/wp-json/vtheme/v1/posts/{id}/delete

Auth required : True

URL Parameters :

  • id (必填): 文章ID

Success Response

Code : 200 OK

Content example :

json
{
  "success": true,
  "message": "文章删除成功"
}

Error Response

Code : 401 Unauthorized

Content examples :

json
{
  "code": "unauthorized",
  "message": "用户未登录",
  "data": {
    "status": 401
  }
}
json
{
  "code": "unauthorized",
  "message": "没有启用投稿",
  "data": {
    "status": 401
  }
}

Code : 403 Forbidden

Content example :

json
{
  "code": "forbidden",
  "message": "没有权限删除此文章",
  "data": {
    "status": 403
  }
}

说明:只有文章作者或管理员可以删除文章

Code : 500 Internal Server Error

Content example :

json
{
  "code": "delete_failed",
  "message": "删除文章失败",
  "data": {
    "status": 500
  }
}

注意事项

1. 投稿功能开关

所有文章相关接口都需要在后台启用"会员投稿"功能才能使用。如果未启用,接口会返回 401 错误:"没有启用投稿"。

2. 文章状态说明

  • private: 私有状态,文章仅作者和管理员可见,通常用于待审核状态
  • draft: 草稿状态,文章保存但不公开
  • pending: 待审核状态,等待管理员审核发布
  • publish: 已发布状态,文章公开可见

默认情况下,新投稿的文章状态为 private

3. 特色图片

  • 特色图片必须是 WordPress 媒体库中的附件(attachment)
  • 需要先通过上传接口上传图片,获取附件ID后再使用
  • 更新文章时,如果 featured_image_id0 或空字符串,会移除当前的特色图片

4. 权限控制

  • 创建文章:需要登录且启用了投稿功能
  • 查看文章列表:只能查看自己发布的文章
  • 更新文章:只能是文章作者或管理员
  • 删除文章:只能是文章作者或管理员

5. 分类验证

  • 提供的分类ID必须存在
  • 使用 get_term_by('term_id', $category_id, 'category') 进行验证
  • 如果不提供分类ID,文章将归类到默认分类

6. 数据验证

  • 标题和内容不能为空
  • 标题会使用 sanitize_text_field() 进行清理
  • 特色图片会验证是否为有效的图片附件
  • 所有输入都会经过 WordPress 的安全过滤

7. 典型使用流程

发布新文章

javascript
// 1. 先上传图片获取附件ID
const uploadResponse = await fetch('/wp-json/wp/v2/media', {
  method: 'POST',
  headers: {
    'Authorization': 'Bearer YOUR_JWT_TOKEN',
    'Content-Disposition': 'attachment; filename="image.jpg"'
  },
  body: imageFile
});
const mediaData = await uploadResponse.json();
const featuredImageId = mediaData.id;

// 2. 发布文章
const postResponse = await fetch('/wp-json/vtheme/v1/posts/create', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_JWT_TOKEN'
  },
  body: JSON.stringify({
    post_title: '我的文章',
    post_content: '<p>文章内容</p>',
    post_category: 5,
    featured_image_id: featuredImageId,
    post_status: 'private'
  })
});

const result = await postResponse.json();
console.log('文章ID:', result.post_id);

获取文章列表

javascript
const response = await fetch('/wp-json/vtheme/v1/posts', {
  headers: {
    'Authorization': 'Bearer YOUR_JWT_TOKEN'
  }
});

const data = await response.json();
console.log('我的文章:', data.posts);

更新文章

javascript
const response = await fetch('/wp-json/vtheme/v1/posts/456/update', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Authorization': 'Bearer YOUR_JWT_TOKEN'
  },
  body: JSON.stringify({
    post_title: '更新后的标题',
    post_content: '<p>更新后的内容</p>',
    featured_image_id: 0  // 移除特色图片
  })
});

const result = await response.json();
console.log(result.message);

删除文章

javascript
const response = await fetch('/wp-json/vtheme/v1/posts/456/delete', {
  method: 'DELETE',
  headers: {
    'Authorization': 'Bearer YOUR_JWT_TOKEN'
  }
});

const result = await response.json();
console.log(result.message);

8. 与WordPress原生API的区别

本接口是专门为会员投稿功能设计的简化版API:

  • 自动处理权限验证
  • 集成投稿开关控制
  • 简化参数结构
  • 统一的响应格式(包含 success 字段)
  • 更适合前端直接使用