Guide

Custom Queries

Extend WPNuxt with your own GraphQL queries

WPNuxt ships with default composables for common WordPress content — posts, pages, menus, and settings. Custom queries let you go beyond the defaults: fetch ACF fields, custom post types, SEO metadata, WooCommerce products, or lighter data shapes that only include the fields you need.

When to Create Custom Queries

ScenarioWhat to Use
Fetch recent posts or pagesDefault composables (usePosts(), usePages())
Fetch posts with extra fields (ACF, SEO, custom taxonomies)Custom query or override default query
Fetch a custom post type (e.g. products, events)Custom query
Fetch a lighter data shape (e.g. only titles and URIs for a listing)Custom query
Change the default number of posts returnedOverride default query

Complete Workflow

1. Check Your Schema

Before writing a query, verify that the fields you need exist in your WordPress GraphQL schema. Visit {your-wordpress-url}/graphql to open the GraphQL IDE and explore available types and fields.

2. Create the Query File

Add a .gql file to extend/queries/:

extend/queries/FeaturedPosts.gql
query FeaturedPosts($limit: Int = 5) {
  posts(first: $limit, where: { tag: "featured" }) {
    nodes {
      ...Post
    }
  }
}
The query name becomes the composable name. query FeaturedPosts generates useFeaturedPosts().

3. Regenerate Types

Run prepare to trigger the composable generation pipeline:

pnpm nuxt prepare

4. Use the Composable

The composable is auto-imported and fully typed:

<script setup lang="ts">
const { data: featured } = await useFeaturedPosts({ limit: 3 })
</script>

<template>
  <article v-for="post in featured" :key="post.id">
    <h2>{{ post.title }}</h2>
  </article>
</template>

5. Verify Types

Hover over featured in your editor — you should see the correct TypeScript types based on the fields in your query and fragment.

Using Fragments

Fragments let you define reusable sets of fields. This is useful when multiple queries need the same fields, or when you want to extend WPNuxt's built-in fragments with additional data.

Place fragment files in extend/queries/fragments/ with a .fragment.gql extension:

extend/queries/fragments/PostWithAuthor.fragment.gql
fragment PostWithAuthor on Post {
  ...Post
  author {
    node {
      name
      avatar {
        url
      }
    }
  }
}

Then use the fragment in a query:

extend/queries/AuthorPosts.gql
query AuthorPosts($authorId: Int!) {
  posts(where: { author: $authorId }) {
    nodes {
      ...PostWithAuthor
    }
  }
}
Naming convention: Name fragment files {FragmentName}.fragment.gql and place them in the fragments/ subfolder. This keeps queries and fragments organized.

Overriding Default Queries

To modify what a default composable returns, create a file with the same name as the default query:

extend/queries/Posts.gql
query Posts($first: Int = 10) {
  posts(first: $first) {
    nodes {
      ...Post
      customField
      seo {
        title
        metaDesc
      }
    }
  }
}

Your version replaces the default. The composable name stays the same — usePosts() will now return your custom fields.

Keep the query name and structure (nodes { ... }) identical to the original. Changing the query name would generate a different composable instead of overriding the existing one.

How Query Merging Works

At build time, WPNuxt merges queries from two sources:

  1. Default queries from @wpnuxt/core (src/runtime/queries/)
  2. Your queries from extend/queries/ (configurable via wpNuxt.queries.extendFolder)

Both are copied to a .queries/ folder. If a file exists in both locations, your version wins. The merged folder is then parsed to generate composables. This is why you can override any default query by matching its filename.

Folder Structure

extend/
└── queries/
    ├── FeaturedPosts.gql           ← new custom query
    ├── Posts.gql                   ← overrides default Posts query
    ├── AuthorPosts.gql             ← new custom query
    └── fragments/
        └── PostWithAuthor.fragment.gql

Troubleshooting

ProblemCauseFix
Composable not generatedTypes not regenerated after adding .gql fileRun pnpm nuxt prepare
Composable not auto-importedQuery file not in extend/queries/Check path matches wpNuxt.queries.extendFolder (default: extend/queries/)
Field returns nullField not exposed in WPGraphQL schemaCheck schema in GraphQL IDE; some fields require plugins (e.g. ACF → WPGraphQL for ACF)
Types are staleOld types cachedDelete .nuxt/ folder and run pnpm nuxt prepare again
Fragment not foundFragment file not in fragments/ subfolder or missing .fragment.gql extensionCheck filename and location
Override not workingQuery file name doesn't match the defaultCheck the exact filename in node_modules/@wpnuxt/core/src/runtime/queries/
Copyright © 2026