Skip to main content
Glama
hafizrahman

Weather & WordPress MCP Server

by hafizrahman

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
NameRequiredDescriptionDefault
categorySlugYesSlug 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")}` }], }; } );
  • Input schema defining 'categorySlug' as a required string parameter.
    { categorySlug: z.string().describe("Slug of the category (e.g., 'technology', 'life')"), },
  • 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")}` }], }; }
  • Helper utility used in formatting post excerpts by stripping HTML tags and replacing &nbsp;.
    function stripHTML(html: string): string { return html.replace(/<\/?[^>]+(>|$)/g, "").replace(/&nbsp;/g, " "); }
  • TypeScript interface for WordPress category objects used in fetching category by slug.
    interface WPCategory { id: number; name: string; slug: string; }

Other Tools

Related Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/hafizrahman/wp-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server