Use ProcessWire Page Reference Fields for Relational Content
Store page relationships by ID and query them with ProcessWire selectors instead of duplicating titles or writing raw SQL.
19 Jul 2026, 04:01 UTC

The problem: titles break, IDs do not
Imagine a Projects section where each project belongs to a client. A common first instinct is to store the client name in a text field or repeat the client title inside the project page. That works until a client page is renamed, moved, or merged. Project pages then show stale names, and finding every project for one client means scanning text fields instead of querying a relationship.
Use page references, not duplicated text
ProcessWire 3.x includes a Page field type, often called a page reference field, that stores the ID of another page rather than its title or path. Editors pick the target from the admin page tree, and ProcessWire keeps the reference valid even if the target page moves.
To query those relationships, use the built-in selector language with $pages->find(). You can filter by template, field values, page references, parents, and dates without writing SQL. ProcessWire returns Page objects with lazy-loaded fields, so unrequested fields are fetched only when your code touches them.
Worked example: link projects to clients
- In the ProcessWire admin, go to Setup > Fields and add a new field named
client. Choose fieldtypePageand, if you have a client template, limit the field to that template. - Go to Setup > Templates, open your
projecttemplate, and add theclientfield. - Create client pages and project pages, selecting a client on each project.
In a template file or the ProcessWire PHP console, run code similar to this:
$clientId = $sanitizer->int($input->get->client);
if (!$clientId) {
throw new \Exception("Missing or invalid client ID.");
}
$projects = $pages->find("template=project, client=$clientId, sort=-created, limit=50");
foreach ($projects as $project) {
echo "<li>" . $project->title . "</li>";
}
You need permission to edit template files or superuser access to the PHP console. The placeholder $clientId must be cast to an integer; passing raw request input into a selector can create risks even though selectors are not SQL.
Expected result: the page list renders only projects whose client field matches the given ID. If nothing appears, confirm that the field is attached to the template and that at least one project page has that client selected.
Debugging the selector
Enable Debug mode in /site/config.php and inspect ProcessWire logs or debug output. This confirms the selector executed against the pages table without raw SQL. Check the installed version in the admin before relying on version-specific selector syntax.
Trade-off: lazy loading can hide extra queries
Because $pages->find() returns lazy-loaded Page objects, accessing fields you did not request can trigger additional database queries inside a loop. If you need many fields, load them up front or limit the result set. Deep page trees or thousands of referenced pages can also make naive find() calls slow, so use limit= and avoid loading unnecessary fields.
Selectors are not SQL. Complex joins or aggregations may require custom repository code or direct database access, which bypasses ProcessWire’s API and hook system.
If you need to remove the field
Adding a field changes the site configuration. To remove the client field later, uninstall it under Setup > Fields, remove it from any templates using it, and confirm no template code still references it. Page reference data is not automatically migrated when the field disappears.
Check the result
- Open a project page in the admin and confirm the client selection saves and displays.
- Load the project list URL with a valid client ID and confirm only related projects appear.
- With Debug mode on, confirm no unexpected database queries appear while rendering the list.
Page reference fields give ProcessWire sites a stable way to model related content. Start with the ID, query with selectors, and watch for lazy-loaded fields as your result sets grow.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.