get-post
Retrieve a specific WordPress post by ID using site URL, credentials, and optional parameters like post password or context (view, embed, edit).
Instructions
Get a specific post by ID
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| context | No | Scope under which the request is made | view |
| password | Yes | WordPress application password | |
| postId | Yes | ID of the post to retrieve | |
| postPassword | No | The password for the post if it is password protected | |
| siteUrl | Yes | WordPress site URL | |
| username | Yes | WordPress username |
Implementation Reference
- src/index.ts:714-744 (handler)The handler function that executes the 'get-post' tool logic. It makes an authenticated API request to retrieve a specific WordPress post by ID, handles optional parameters like context and postPassword, formats the post details into a text response, and catches any errors.async ({ siteUrl, username, password, postId, context, postPassword }) => { try { const params: Record<string, any> = { context }; if (postPassword) params.password = postPassword; const post = await makeWPRequest<WPPost>({ siteUrl, endpoint: `posts/${postId}`, auth: { username, password }, params }); return { content: [ { type: "text", text: `Post Details:\nID: ${post.id}\nTitle: ${post.title?.rendered || "No title"}\nDate: ${post.date || "No date"}\nStatus: ${post.status || "unknown"}\nAuthor: ${post.author || "Unknown"}\nContent: ${post.content?.rendered || "No content"}\nExcerpt: ${post.excerpt?.rendered || "No excerpt"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving post: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } }
- src/index.ts:706-713 (schema)Zod input schema defining the parameters for the 'get-post' tool: required siteUrl, username, password, postId; optional context and postPassword.{ siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), postId: z.number().describe("ID of the post to retrieve"), context: z.enum(["view", "embed", "edit"]).optional().default("view").describe("Scope under which the request is made"), postPassword: z.string().optional().describe("The password for the post if it is password protected"), },
- src/index.ts:704-745 (registration)Registration of the 'get-post' tool on the MCP server using server.tool(), specifying name, description, input schema, and handler function."get-post", "Get a specific post by ID", { siteUrl: z.string().url().describe("WordPress site URL"), username: z.string().describe("WordPress username"), password: z.string().describe("WordPress application password"), postId: z.number().describe("ID of the post to retrieve"), context: z.enum(["view", "embed", "edit"]).optional().default("view").describe("Scope under which the request is made"), postPassword: z.string().optional().describe("The password for the post if it is password protected"), }, async ({ siteUrl, username, password, postId, context, postPassword }) => { try { const params: Record<string, any> = { context }; if (postPassword) params.password = postPassword; const post = await makeWPRequest<WPPost>({ siteUrl, endpoint: `posts/${postId}`, auth: { username, password }, params }); return { content: [ { type: "text", text: `Post Details:\nID: ${post.id}\nTitle: ${post.title?.rendered || "No title"}\nDate: ${post.date || "No date"}\nStatus: ${post.status || "unknown"}\nAuthor: ${post.author || "Unknown"}\nContent: ${post.content?.rendered || "No content"}\nExcerpt: ${post.excerpt?.rendered || "No excerpt"}`, }, ], }; } catch (error) { return { content: [ { type: "text", text: `Error retrieving post: ${error instanceof Error ? error.message : String(error)}`, }, ], }; } } );
- src/index.ts:10-57 (helper)TypeScript interface WPPost defining the structure of WordPress post objects used in the get-post tool response typing.interface WPPost { id: number; date?: string; date_gmt?: string; guid?: { rendered: string; }; modified?: string; modified_gmt?: string; slug?: string; status?: 'publish' | 'future' | 'draft' | 'pending' | 'private'; type?: string; link?: string; title?: { rendered: string; }; content?: { rendered: string; protected?: boolean; }; excerpt?: { rendered: string; protected?: boolean; }; author?: number; featured_media?: number; comment_status?: 'open' | 'closed'; ping_status?: 'open' | 'closed'; sticky?: boolean; template?: string; format?: 'standard' | 'aside' | 'chat' | 'gallery' | 'link' | 'image' | 'quote' | 'status' | 'video' | 'audio'; meta?: Record<string, any>; _links?: { self?: Array<{ href: string }>; collection?: Array<{ href: string }>; about?: Array<{ href: string }>; author?: Array<{ href: string; embeddable: boolean }>; replies?: Array<{ href: string; embeddable: boolean }>; 'version-history'?: Array<{ href: string }>; 'predecessor-version'?: Array<{ href: string; id: number }>; 'wp:featuredmedia'?: Array<{ href: string; embeddable: boolean }>; 'wp:attachment'?: Array<{ href: string }>; 'wp:term'?: Array<{ href: string; taxonomy: string; embeddable: boolean }>; curies?: Array<{ name: string; href: string; templated: boolean }>; }; categories?: number[]; tags?: number[]; }
- src/index.ts:158-194 (helper)Shared helper function makeWPRequest that performs authenticated HTTP requests to the WordPress REST API, used by the get-post handler.async function makeWPRequest<T>({ siteUrl, endpoint, method = 'GET', auth, data = null, params = null }: { siteUrl: string; endpoint: string; method?: 'GET' | 'POST' | 'PUT' | 'DELETE'; auth: { username: string; password: string }; data?: any; params?: any; }): Promise<T> { const authString = Buffer.from(`${auth.username}:${auth.password}`).toString('base64'); try { const response = await axios({ method, url: `${siteUrl}/wp-json/wp/v2/${endpoint}`, headers: { 'Authorization': `Basic ${authString}`, 'Content-Type': 'application/json', }, data: data, params: params }); return response.data as T; } catch (error) { if (axios.isAxiosError(error) && error.response) { throw new Error(`WordPress API error: ${error.response.data?.message || error.message}`); } throw error; } }