get-posts-by-category
Retrieve WordPress posts from hafiz.blog by category slug to filter content for specific topics like technology or life.
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:250-286 (handler)The handler function fetches the category ID by slug from the WordPress API, retrieves the 10 most recent posts in that category, formats each with title, date, link, and excerpt (HTML stripped), and returns them as a markdown-like text block.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 the required 'categorySlug' parameter as a string, with description providing examples.{ categorySlug: z.string().describe("Slug of the category (e.g., 'technology', 'life')"), },
- src/index.ts:244-286 (registration)Full tool registration call using server.tool(), specifying name, description, input schema, and inline handler function.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:175-177 (helper)Helper function used to clean HTML from post excerpts before including in the output.function stripHTML(html: string): string { return html.replace(/<\/?[^>]+(>|$)/g, "").replace(/ /g, " "); }
- src/index.ts:20-28 (helper)Shared utility function for making JSON API requests with error handling, used for fetching categories and posts.async function fetchJson<T>(url: string, headers: Record<string, string> = {}): Promise<T | null> { try { const response = await fetch(url, { headers: { "User-Agent": USER_AGENT, ...headers } }); if (!response.ok) throw new Error(`HTTP error ${response.status}`); return (await response.json()) as T; } catch (err) { console.error("Fetch error:", err); return null; }