get_page_id_by_slug
Retrieve the WordPress page ID using its slug for targeted Elementor data management. Input the slug to identify the specific page within your WordPress site.
Instructions
Retrieves the ID of a specific WordPress page by its slug.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| slug | Yes | The slug (URL-friendly name) of the page to find. |
Implementation Reference
- src/index.js:267-278 (handler)The handler function for the MCP tool 'get_page_id_by_slug'. It takes the input slug, calls the getPageIdBySlug helper, and returns the page ID formatted as MCP text content.async (input) => { // Handler const pageId = await getPageIdBySlug(input.slug); return { content: [ { type: "text", text: `${pageId}`, }, ], }; }
- src/index.js:261-266 (schema)Zod schema defining the input parameters for the tool: a required 'slug' string.{ // Input Schema slug: z .string() .describe("The slug (URL-friendly name) of the page to find."), },
- src/index.js:258-279 (registration)Registration of the 'get_page_id_by_slug' tool using server.tool(), including name, description, schema, and inline handler.server.tool( "get_page_id_by_slug", "Retrieves the ID of a specific WordPress page by its slug.", { // Input Schema slug: z .string() .describe("The slug (URL-friendly name) of the page to find."), }, async (input) => { // Handler const pageId = await getPageIdBySlug(input.slug); return { content: [ { type: "text", text: `${pageId}`, }, ], }; } );
- src/wp-api.js:79-102 (helper)Supporting helper function that queries the WordPress REST API for pages matching the slug, returns the first ID found, or throws if none.async function getPageIdBySlug(slug) { const client = getApiClient(); let pageId = null; try { const response = await client.get(`/wp-json/wp/v2/pages`, { params: { slug: slug, _fields: 'id', // Only need the ID } }); if (response.data && response.data.length > 0) { pageId = response.data[0].id; return pageId; // Return the ID directly } else { // Throw an error if not found, to be caught by the caller throw new Error(`Page with slug '${slug}' not found.`); } } catch (error) { // Re-throw the error for the caller to handle throw error; // Propagate the error (could be Axios error or the 'not found' error) } }