Skip to main content

Function Signature

Fetches a single WordPress page from the CMS API using its slug identifier. This function retrieves the page with embedded media data and transforms it into the application’s WPPage format.

Parameters

string
required
The URL-friendly slug of the WordPress page to fetch. The slug is automatically URL-encoded to handle special characters.

Return Type

Promise<WPPage | null>
Returns a Promise that resolves to a WPPage object if found, or null if no page matches the slug.

Behavior

  • Makes a GET request to the WordPress REST API /pages endpoint
  • Includes _embed parameter to fetch related media data
  • URL-encodes the slug parameter for safe transmission
  • Returns null if no page is found with the given slug
  • Strips HTML tags from title and excerpt fields
  • Formats dates using Spanish locale (es-ES)
  • Returns empty string for image if featured media is not available
  • Throws an error if the API request fails

Error Handling

Throws an error with the format:

Usage Example

Basic Usage

With Null Checking

Router Integration

With Try-Catch

API Endpoint

The function connects to: