Fetching Data

Basic Fetch

Use {{ fetch }} when WordPress should request JSON from an API while rendering the page.

Basic Shape#

phpresources/views/front-page.php
{{ fetch "https://api.example.com/reviews" }}  <article>    <h2>{{ title }}</h2>    <p>{{ author.name }}</p>  </article>{{ /fetch }}

The fetch tag is a paired tag. The opening tag contains the API URL. The closing tag ends the fetched data loop.

Inside the block, value tags read from the current JSON item:

  • {{ title }} reads the title key.
  • {{ author.name }} reads the nested name key inside author.

Lists And Objects#

If the selected JSON value is a list, the markup repeats once for each item.

If the selected JSON value is one object, the markup renders once.

If the response is empty, invalid, or unavailable, the block renders nothing unless you add an {{ else }} branch.

Runtime Cache#

{{ fetch }} is a runtime fetch. WordPress requests the API when the page renders, then TemplateX caches that response briefly so the same URL is not requested on every page load.

Use runtime fetches for API data that should be checked regularly.

Rules#

  • The URL must start with http:// or https://.
  • The response must be valid JSON.
  • The HTML inside the block is your markup.