get-posts-by-category
Fetch posts from hafiz.blog by specifying a category slug, enabling targeted content retrieval for WordPress.com users. Ideal for organizing and accessing categorized blog posts.
Instructions
Get posts from a specific category on hafiz.blog (WordPress.com)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| categorySlug | Yes | Slug of the category (e.g., 'technology', 'life') |
Implementation Reference
- src/index.ts:244-286 (registration)Registration of the 'get-posts-by-category' tool, including input schema and the complete handler function that fetches category by slug to get ID, then retrieves and formats posts.server.tool( "get-posts-by-category", "Get posts from a specific category on hafiz.blog (WordPress.com)", { categorySlug: z.string().describe("Slug of the category (e.g., 'technology', 'life')"), }, async ({ categorySlug }) => { // Get category ID using the category slug const categoriesUrl = `${WP_COM_API_BASE}/categories?slug=${categorySlug}`; const categories = await fetchJson<WPCategory[]>(categoriesUrl); if (!categories || categories.length === 0) { return { content: [{ type: "text", text: `No category found with slug '${categorySlug}'` }], }; } // Fetch posts based on the category ID const categoryId = categories[0].id; const postsUrl = `${WP_COM_API_BASE}/posts?categories=${categoryId}&per_page=10&_fields=id,title,link,date,excerpt`; const posts = await fetchJson<WPPost[]>(postsUrl); if (!posts || posts.length === 0) { return { content: [{ type: "text", text: `No posts found in category '${categorySlug}'` }], }; } const formattedPosts = posts.map((post) => [ `Title: ${post.title.rendered}`, `Date: ${new Date(post.date).toLocaleString()}`, `Link: ${post.link}`, `Excerpt: ${stripHTML(post.excerpt.rendered).slice(0, 200)}...`, "---", ].join("\n") ); return { content: [{ type: "text", text: `Posts in category '${categorySlug}':\n\n${formattedPosts.join("\n")}` }], }; } );
- src/index.ts:247-249 (schema)Input schema defining 'categorySlug' as a required string parameter.{ categorySlug: z.string().describe("Slug of the category (e.g., 'technology', 'life')"), },
- src/index.ts:250-285 (handler)Handler logic: Retrieves category ID via slug, fetches up to 10 posts from that category using WordPress REST API, formats them with title, date, link, and excerpt (stripped HTML), returns as text content.async ({ categorySlug }) => { // Get category ID using the category slug const categoriesUrl = `${WP_COM_API_BASE}/categories?slug=${categorySlug}`; const categories = await fetchJson<WPCategory[]>(categoriesUrl); if (!categories || categories.length === 0) { return { content: [{ type: "text", text: `No category found with slug '${categorySlug}'` }], }; } // Fetch posts based on the category ID const categoryId = categories[0].id; const postsUrl = `${WP_COM_API_BASE}/posts?categories=${categoryId}&per_page=10&_fields=id,title,link,date,excerpt`; const posts = await fetchJson<WPPost[]>(postsUrl); if (!posts || posts.length === 0) { return { content: [{ type: "text", text: `No posts found in category '${categorySlug}'` }], }; } const formattedPosts = posts.map((post) => [ `Title: ${post.title.rendered}`, `Date: ${new Date(post.date).toLocaleString()}`, `Link: ${post.link}`, `Excerpt: ${stripHTML(post.excerpt.rendered).slice(0, 200)}...`, "---", ].join("\n") ); return { content: [{ type: "text", text: `Posts in category '${categorySlug}':\n\n${formattedPosts.join("\n")}` }], }; }
- src/index.ts:175-177 (helper)Helper utility used in formatting post excerpts by stripping HTML tags and replacing .function stripHTML(html: string): string { return html.replace(/<\/?[^>]+(>|$)/g, "").replace(/ /g, " "); }
- src/index.ts:209-213 (helper)TypeScript interface for WordPress category objects used in fetching category by slug.interface WPCategory { id: number; name: string; slug: string; }