wp_create_post
Create WordPress posts with validation and management links. Set titles, content, status, categories, tags, featured images, and scheduling.
Instructions
Creates a new WordPress post with comprehensive validation and detailed success feedback including management links.
Usage Examples:
• Simple post: wp_create_post --title="My New Post" --content="<p>Hello World!</p>"
• Draft post: wp_create_post --title="Draft Post" --status="draft"
• Categorized post: wp_create_post --title="Tech News" --categories=[1,5] --tags=[10,20]
• Post with featured image: wp_create_post --title="My Post" --content="<p>Content</p>" --featured_media=42
• Remove featured image: wp_create_post --title="My Post" --featured_media=0
• Scheduled post: wp_create_post --title="Future Post" --status="future" --date="2024-12-25T10:00:00"
• Complete post: wp_create_post --title="Complete Post" --content="<p>Content</p>" --excerpt="Summary" --status="publish"
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| site | No | The ID of the WordPress site to target (from mcp-wordpress.config.json). Required if multiple sites are configured. | |
| title | Yes | The title for the post. | |
| content | No | The content for the post, in HTML format. | |
| status | No | The publishing status for the post. | |
| excerpt | No | The excerpt for the post. | |
| categories | No | An array of category IDs to assign to the post. | |
| tags | No | An array of tag IDs to assign to the post. | |
| featured_media | No | The ID of the featured media (image). Use 0 to remove featured media. | |
| date | No | The date the post was published, in the site's timezone (ISO 8601 format). |
Implementation Reference
- src/tools/posts/PostHandlers.ts:320-351 (handler)Core handler implementation for wp_create_post tool. Validates input parameters, creates the post via WordPressClient, and returns formatted success response with post details and admin links.export async function handleCreatePost( client: WordPressClient, params: CreatePostRequest, ): Promise<WordPressPost | string> { try { validatePostParams(params); const post = await client.createPost(params); // Build success response with management links let response = `✅ **Post Created Successfully**\n\n`; response += `**Title**: ${post.title.rendered}\n`; response += `**ID**: ${post.id}\n`; response += `**Status**: ${post.status}\n`; response += `**Link**: ${post.link}\n`; // Add management links const siteUrl = client.getSiteUrl ? client.getSiteUrl() : ""; if (siteUrl) { response += `\n**Management**:\n`; response += `- Edit: ${siteUrl}/wp-admin/post.php?post=${post.id}&action=edit\n`; if (post.status === "publish") { response += `- View: ${post.link}\n`; } else { response += `- Preview: ${siteUrl}/?p=${post.id}&preview=true\n`; } } return response; } catch (_error) { throw new Error(`Failed to create post: ${getErrorMessage(_error)}`); } }
- Tool schema definition including name, detailed description with usage examples, and inputSchema with parameters, validation (required title, optional others), and enums.export const createPostTool: MCPTool = { name: "wp_create_post", description: "Creates a new WordPress post with comprehensive validation and detailed success feedback including management links.\n\n" + "**Usage Examples:**\n" + '• Simple post: `wp_create_post --title="My New Post" --content="<p>Hello World!</p>"`\n' + '• Draft post: `wp_create_post --title="Draft Post" --status="draft"`\n' + '• Categorized post: `wp_create_post --title="Tech News" --categories=[1,5] --tags=[10,20]`\n' + '• Post with featured image: `wp_create_post --title="My Post" --content="<p>Content</p>" --featured_media=42`\n' + '• Remove featured image: `wp_create_post --title="My Post" --featured_media=0`\n' + '• Scheduled post: `wp_create_post --title="Future Post" --status="future" --date="2024-12-25T10:00:00"`\n' + '• Complete post: `wp_create_post --title="Complete Post" --content="<p>Content</p>" --excerpt="Summary" --status="publish"`', inputSchema: { type: "object", properties: { title: { type: "string", description: "The title for the post.", }, content: { type: "string", description: "The content for the post, in HTML format.", }, status: { type: "string", description: "The publishing status for the post.", enum: ["publish", "draft", "pending", "private"], }, excerpt: { type: "string", description: "The excerpt for the post.", }, categories: { type: "array", items: { type: "number" }, description: "An array of category IDs to assign to the post.", }, tags: { type: "array", items: { type: "number" }, description: "An array of tag IDs to assign to the post.", }, featured_media: { type: "number", description: "The ID of the featured media (image). Use 0 to remove featured media.", }, date: { type: "string", description: "The date the post was published, in the site's timezone (ISO 8601 format).", }, }, required: ["title"], }, };
- src/tools/posts/index.ts:90-107 (registration)Registration mapping: binds the tool name 'wp_create_post' to the handleCreatePost method in the PostTools class's getHandlerForTool switch statement, used by getTools() to attach handlers to tool definitions.private getHandlerForTool(toolName: string) { switch (toolName) { case "wp_list_posts": return this.handleListPosts.bind(this); case "wp_get_post": return this.handleGetPost.bind(this); case "wp_create_post": return this.handleCreatePost.bind(this); case "wp_update_post": return this.handleUpdatePost.bind(this); case "wp_delete_post": return this.handleDeletePost.bind(this); case "wp_get_post_revisions": return this.handleGetPostRevisions.bind(this); default: throw new Error(`Unknown tool: ${toolName}`); } }
- src/tools/posts/index.ts:177-195 (handler)Wrapper handler in PostTools class that extracts and type-casts MCP input parameters before delegating to the core handleCreatePost function.public async handleCreatePost( client: WordPressClient, params: CreatePostRequest | Record<string, unknown>, ): Promise<WordPressPost | string> { // Extract only the relevant post creation parameters, excluding MCP-specific fields like 'site' const postParams: CreatePostRequest = { title: params.title as string, }; if (params.content !== undefined) postParams.content = params.content as string; if (params.status !== undefined) postParams.status = params.status as PostStatus; if (params.excerpt !== undefined) postParams.excerpt = params.excerpt as string; if (params.categories !== undefined) postParams.categories = params.categories as number[]; if (params.tags !== undefined) postParams.tags = params.tags as number[]; if (params.featured_media !== undefined) postParams.featured_media = params.featured_media as number; if (params.date !== undefined) postParams.date = params.date as string; return handleCreatePost(client, postParams); }