Pages / $pages
The $pages API variable loads, creates, saves and deletes Page objects to and from the database. It is the most frequently used API variable in ProcessWire
This document covers the most commonly
used methods of $pages but it does not cover them all. See /wire/core/Pages/Pages.php and in /wire/core/Pages/*.php phpdoc for full methods reference, or the online
Pages API reference.
// Find all pages matching a selector — returns PageArray
$items = $pages->find("template=blog-post, sort=-created, limit=10");
// Find one page — applies same access/status filtering as find()
$p = $pages->findOne("template=blog-post, sort=random");
// Get a page with no access/status exclusions
$p = $pages->get(1234); // by ID
$p = $pages->get('/about/'); // by path
$p = $pages->get('name=contact'); // by selector
// Returns NullPage (id=0) when nothing found — always check before use
if($p->id) { /* found */ }
// Count matching pages without loading them
$n = $pages->count("template=blog-post, date>=2024-01-01");
// Check existence with no access/status exclusions
// Returns first matching page ID or 0 if not found
$id = $pages->has("template=blog-post, name=foo");
// Iterate any number of pages without running out of memory
foreach($pages->findMany("template=product") as $p) {
// pages are loaded/unloaded in chunks as you iterate
}Selectors
Selector format: field=value — combine multiple with commas for AND match:
$pages->find("template=blog-post, date>=2024-01-01, title~=processwire");Use pipe | on field for OR-field matches or value for OR-value matches:
// match word "processwire" in title, summary or body
$pages->find("title|summary|body~=processwire");
// match word "process" or "wire" or "processwire" in title
$pages->find("title~=process|wire|processwire"); Most common operators: =, !=, <, <=, >, >=, ~= (contains word)
%= (contains using LIKE), *= (contains using index), ^= (starts), $= (ends). See Selector Operators for a full list.
Include modes:
$pages->find("template=blog-post, include=hidden"); // include hidden
$pages->find("template=blog-post, include=unpublished"); // include unpublished + hidden
$pages->find("template=blog-post, include=all"); // include all + bypass access controlfind() modifiers in selector
Common selector modifiers for find(), findOne(), get(), and related methods:
| Option | Default | Description |
|---|---|---|
include=status | — | Status may be hidden, unpublished, or all — include non-visible pages |
limit=n | 0 | Max pages to return or 0 for no limit (also enables $items->getTotal() count) |
start=n | 0 | Pagination offset/start value |
sort=name | — | Field name to sort by, prefix name with - to reverse |
join=fields | — | Fields should be pipe-separated list of field names to autojoin (where supported) |
Specialized find methods
// Returns IDs only (faster when you don't need Page objects)
$ids = $pages->findIDs("template=product");
// Verbose: returns array of ['id', 'parent_id', 'templates_id'] per match
$ids = $pages->findIDs("template=product", true);
// Specify which fields to autojoin (overriding field autojoin settings)
$posts = $pages->findJoin("template=blog-post", ['title', 'date', 'summary']);
// No autojoin at all
$posts = $pages->findJoin("template=blog-post", false);
// Raw DB values — no Page objects, no output formatting
$a = $pages->findRaw("template=blog-post", ['title', 'date']);
$a = $pages->getRaw(1234, ['title', 'summary']);
// Get a non-cached copy of a page (fresh from DB, skips memory cache)
$copy = $pages->getFresh($page);// Quick method — template, parent, optional name/title/values
$p = $pages->add('blog-post', '/blog/', 'My First Post');
// With field values
$p = $pages->add('blog-post', '/blog/', [
'title' => 'My First Post',
'body' => 'Hello world.',
]);
// Selector-style interface — saves to DB immediately
$p = $pages->new("template=blog-post, parent=/blog/, title=My First Post");
$p = $pages->new('/blog/my-first-post'); // path implies parent+name+template (if family allows)
$p = $pages->new([
'template' => 'blog-post',
'parent' => '/blog/',
'title' => 'My First Post',
]);
// Create a Page object without saving to DB
$p = $pages->newPage(['template' => 'blog-post', 'parent' => '/blog/']);$pages->new() auto-detects template from parent family settings (and vice versa), derives
name from title or path, and appends a numeric suffix if the name is already taken.
$p = $pages->get(1234);
$p->of(false); // turn off output formatting before editing
$p->title = 'Updated title';
$pages->save($p); // or: $p->save()
// Save only specific fields (faster)
$pages->saveField($p, 'title');
$pages->saveFields($p, ['title', 'body', 'summary']); // 3.0.242+
$pages->saveFields($p, 'title, body, summary'); // CSV string also ok
// Clone a page (recursive by default — also clones children and file assets)
$copy = $pages->clone($p);
$copy = $pages->clone($p, $newParent, false); // non-recursive
// Update modification time to now
$pages->touch($p);
// Set sort order
$pages->sort($p, 3); // move to position 3 among siblings
$pages->insertBefore($p, $beforePage);
$pages->insertAfter($p, $afterPage);save() options
| Option | Default | Description |
|---|---|---|
uncacheAll | true | Clear memory cache after save |
resetTrackChanges | true | Reset page's change tracking |
quiet | false | Skip updating modified date/user |
adjustName | true | Auto-adjust name to be unique within parent |
ignoreFamily | false | Bypass family/parent restriction checks |
noHooks | false | Skip before/after save hooks |
noFields | false | Save only native page properties, not fields |
// Move to trash (recoverable)
$pages->trash($p);
// Restore from trash to original location
$pages->restore($p);
// Empty the entire trash (permanent)
$pages->emptyTrash();
// Permanently delete (non-recoverable)
$pages->delete($p);
$pages->delete($p, true); // recursive — also deletes childrenProcessWire maintains an in-memory cache of loaded pages keyed by ID. This cache is consulted
before hitting the database on any get()/find() call.
// Remove specific pages from the memory cache
$pages->uncache($p);
$pages->uncache($pageArray);
$pages->uncache([1234, 1235, 1236]); // array of IDs 3.0.259+
// Clear entire memory cache — useful when processing large sets
$pages->uncacheAll();uncacheAll() is typically used inside pagination loops that process thousands of pages, so that
previous chunks can be freed:
$start = 0;
$limit = 200;
do {
$chunk = $pages->find("template=product, start=$start, limit=$limit");
if(!$chunk->count()) break;
foreach($chunk as $p) { /* process */ }
unset($chunk);
$pages->uncacheAll();
$start += $limit;
} while(true);$p = $pages->newPage(); // new unsaved Page (no template)
$p = $pages->newPage(['template' => 'foo']); // with template/parent pre-set
$pa = $pages->newPageArray(); // empty PageArray
$null = $pages->newNullPage(); // new NullPage instanceUseful hooks on $pages:
| Hook | When fired |
|---|---|
Pages::found | After find completes, receives the full PageArray |
Pages::saveReady | Just before a page is saved |
Pages::saved | After a page is successfully saved |
Pages::savePageOrFieldReady | Before a page or a field is saved |
Pages::savedPageOrField | After a page or a field is saved |
Pages::addReady | Before a new page is added |
Pages::added | After a new page is added |
Pages::deleteReady | Before a page is deleted |
Pages::deleted | After a page is deleted |
Pages::trashReady | Before a page is trashed |
Pages::trashed | After a page is trashed |
Pages::restoreReady | Before a page is restored from trash |
Pages::restored | After a page is restored from trash |
Pages::cloneReady | Before a page is cloned |
Pages::cloned | After a page is cloned |
Other useful Pages hooks include: moveReady, moved, sorted,
templateChanged, renameReady, renamed, publishReady, published,
unpublishReady, unpublished, saveFieldReady, and savedField.
In addition, all $pages write methods can also be hooked directly with
addHookBefore() or addHookAfter().
// Example: update field value and status on every newly added page
$wire->addHookAfter('Pages::added', function(HookEvent $e) {
$page = $e->arguments(0); /** @var Page $page */
if($page->template->name === 'blog-post') {
$page->categories->add('/categories/pending-review');
$page->addStatus('hidden');
$page->save();
}
});
// $pages can also be hooked directly like this
$pages->addHookAfter('added', function(HookEvent $e) {
// ...
}); The $pages object delegates to several helper classes. These are lazy-loaded and should not be accessed directly from the public API unless they provide a method not
available on $pages, or if otherwise directed.
| Property | Class | Purpose |
|---|---|---|
$pages->loader | PagesLoader | All find/get/load operations |
$pages->editor | PagesEditor | save, add, delete, clone |
$pages->cacher | PagesLoaderCache | In-memory page cache |
$pages->trasher | PagesTrash | trash, restore, emptyTrash |
$pages->names | PagesNames | Page name generation and uniqueness |
$pages->raw | PagesRaw | findRaw / getRaw |
$pages->pathFinder | PagesPathFinder | Path-to-page resolution |
$pages->request | PagesRequest | Current HTTP request page resolution |
$pages->parents | PagesParents | Ancestor/parent tree queries |
$pages->porter | PagesExportImport | Import/export |
$pages->get()never excludes pages by status or access — it returns whatever matches, including hidden, unpublished, and admin pages. Use$pages->findOne()if you need access/status filtering.- Output formatting is on for front-end requests and off in the admin. Always call
$page->of(false)before modifying and saving a page that may have been loaded in a front-end context. $pages->newNullPage()returns a new NullPage instance.- All
$pageswrite methods (save,add,delete,trash, etc.) are hookable via the triple-underscore___methodName()pattern.
Manages Page instances, providing find, load, save and delete capabilities.
The implementation for most of the methods in this class are delegated to other classes (helpers)
but this class provides the common and hookable interface to all of them.
The $pages API variable is the most used object in the ProcessWire API. The most commonly used API methods include:
$pages->find($selector);Finds and returns multiple pages.$pages->get($selector);Finds a single page with no exclusions.$pages->save($page);Saves given page.
Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the Pages class also inherits all the methods and properties of: Wire.
- Retrieve
- Save
- Add
- Trash
- Move
- Advanced
- Cache
- Helpers
- Common
- Save hooks
- Add hooks
- Move hooks
- Trash hooks
- Delete hooks
- Status hooks
- Find hooks
Retrieve
Save
| Name | Return | Summary | |
|---|---|---|---|
$pages->save() $pages->save(Page $page) $pages->save(Page $page, array $options = []) | bool | Save a page object and its fields to database. | |
$pages->saveField() $pages->saveField(Page $page, $field) $pages->saveField(Page $page, $field, $options = []) | bool | Save only a field from the given page | |
$pages->saveFields() $pages->saveFields(Page $page, $fields) $pages->saveFields(Page $page, $fields, array $options = []) | array | Save multiple named fields from given page | |
$pages->touch() $pages->touch($pages) $pages->touch($pages, $options = null, string $type = 'modified') | bool | Update page modification time to now (or the given modification time) |
Add
| Name | Return | Summary | |
|---|---|---|---|
$pages->add() $pages->add($template, $parent) $pages->add($template, $parent, string $name = '', array $values = []) | Page | Add a new page using the given template and parent | |
$pages->clone() $pages->clone(Page $page) $pages->clone(Page $page, $parent = null, bool $recursive = true, $options = []) | Page NullPage | Clone entire page and return it | |
$pages->new() $pages->new() $pages->new($selector = '') | Page | Create a new Page populated from selector string or array and save to database | |
$pages->newPage() $pages->newPage() $pages->newPage($options = []) | Page | Return a new Page object without saving it to the database |
Trash
| Name | Return | Summary | |
|---|---|---|---|
$pages->delete() $pages->delete(Page $page) $pages->delete(Page $page, $recursive = false, array $options = []) | bool int | Permanently delete a page, its fields and assets. | |
$pages->emptyTrash() $pages->emptyTrash() $pages->emptyTrash(array $options = []) | int array | Delete all pages in the trash | |
$pages->restore() $pages->restore(Page $page) $pages->restore(Page $page, bool $save = true) | bool | Restore a page in the trash back to its original location and state | |
$pages->trash() $pages->trash(Page $page) $pages->trash(Page $page, bool $save = true) | bool | Move a page to the trash |
Move
| Name | Return | Summary | |
|---|---|---|---|
$pages->insertAfter() $pages->insertAfter(Page $page, Page $afterPage) $pages->insertAfter(Page $page, Page $afterPage) | None | Sort/move one page after another (for manually sorted pages) | |
$pages->insertBefore() $pages->insertBefore(Page $page, Page $beforePage) $pages->insertBefore(Page $page, Page $beforePage) | None | Sort/move one page above another (for manually sorted pages) | |
$pages->sort() $pages->sort(Page $page) $pages->sort(Page $page, $value = false) | int | Set the “sort” value for given $page while adjusting siblings, or re-build sort for its children |
Advanced
Cache
| Name | Return | Summary | |
|---|---|---|---|
$pages->getCache() $pages->getCache() $pages->getCache($id = null) | Page array null | Given a Page ID, return it if it's cached, or NULL of it's not. | |
$pages->uncache() $pages->uncache() $pages->uncache($page = null, array $options = []) | int | Remove the given page(s) from the cache, or uncache all by omitting $page argument | |
$pages->uncacheAll() $pages->uncacheAll() $pages->uncacheAll($options = [], $deprecated = null) | int | Remove all pages from the cache (to clear memory) |
Helpers
Methods that point to dedicated Pages helper classes. Use methods from $pages rather than the helpers when there is crossover.
| Name | Return | Summary | |
|---|---|---|---|
$pages->cacher() $pages->cacher() $pages->cacher() | PagesLoaderCache | Get PagesLoaderCache instance which provides methods for caching pages in memory | |
$pages->editor() $pages->editor() $pages->editor() | PagesEditor | Get PagesEditor instance which provides methods for saving pages to the database | |
$pages->loader() $pages->loader() $pages->loader() | PagesLoader | Get PagesLoader instance which provides methods for finding and loading pages | |
$pages->names() $pages->names() $pages->names() | PagesNames | Get PagesNames instance which provides API methods specific to generating and modifying page names | |
$pages->parents() $pages->parents() $pages->parents() | PagesParents | Get PagesParents instance which provides methods for managing parent/child relationships in the pages_parents table | |
$pages->pathFinder() $pages->pathFinder() $pages->pathFinder() | PagesPathFinder | Get the PagesPathFinder instance which provides methods for finding pages by paths | |
$pages->porter() $pages->porter() $pages->porter() | PagesExportImport | Get new instance of PagesExportImport for exporting and importing pages | |
$pages->raw() $pages->raw() $pages->raw() | PagesRaw | Get the PagesRaw instance which provides methods for finding and loading raw pages data | |
$pages->request() $pages->request() $pages->request() | PagesRequest | Get the PagesRequest instance which provides methods for identifying and loading page from current request URL | |
$pages->trasher() $pages->trasher() $pages->trasher() | PagesTrash | Get PagesTrash instance which provides methods for managing the Pages trash |
Common
| Name | Return | Summary | |
|---|---|---|---|
$pages->newNullPage() $pages->newNullPage() $pages->newNullPage(bool $forceNew = false) | NullPage | Return a NullPage |
Save hooks
Methods called automatically when a page is saved. You can hook these methods but should not call them directly.
Add hooks
Methods called automatically when a page is added. You can hook these methods but should not call them directly.
| Name | Return | Summary | |
|---|---|---|---|
$pages->addReady() $pages->addReady(Page $page) $pages->addReady(Page $page) | None | Hook called when a new page is about to be added and saved to the database | |
$pages->added() $pages->added(Page $page) $pages->added(Page $page) | None | Hook called after a new page has been added | |
$pages->cloneReady() $pages->cloneReady(Page $page, Page $copy) $pages->cloneReady(Page $page, Page $copy) | None | Hook called when a page is about to be cloned, but before data has been touched | |
$pages->cloned() $pages->cloned(Page $page, Page $copy) $pages->cloned(Page $page, Page $copy) | None | Hook called when a page has been cloned |
Move hooks
Methods called automatically when a page is moved, sorted or renamed. You can hook these methods but should not call them directly.
| Name | Return | Summary | |
|---|---|---|---|
$pages->moveReady() $pages->moveReady(Page $page) $pages->moveReady(Page $page) | None | Hook called when a page is about to be moved to another parent | |
$pages->moved() $pages->moved(Page $page) $pages->moved(Page $page) | None | Hook called when a page has been moved from one parent to another | |
$pages->renameReady() $pages->renameReady(Page $page) $pages->renameReady(Page $page) | None | Hook called when a page is about to be renamed i.e. had its name field change) | |
$pages->renamed() $pages->renamed(Page $page) $pages->renamed(Page $page) | None | Hook called when a page has been renamed (i.e. had its name field change) | |
$pages->sorted() $pages->sorted(Page $page) $pages->sorted(Page $page, bool $children = false, int $total = 0) | None | Hook called after a page has been sorted, or had its children re-sorted |
Trash hooks
Methods called automatically when a page is trashed or restored. You can hook these methods but should not call them directly.
| Name | Return | Summary | |
|---|---|---|---|
$pages->restoreReady() $pages->restoreReady(Page $page) $pages->restoreReady(Page $page) | None | Hook called when a page is about to be moved OUT of the trash (restored) | |
$pages->restored() $pages->restored(Page $page) $pages->restored(Page $page) | None | Hook called when a page has been moved OUT of the trash (restored) | |
$pages->trashReady() $pages->trashReady(Page $page) $pages->trashReady(Page $page) | None | Hook called when a Page is about to be trashed | |
$pages->trashed() $pages->trashed(Page $page) $pages->trashed(Page $page) | None | Hook called when a page has been moved to the trash |
Delete hooks
Methods called automatically when a page is deleted. You can hook these methods but should not call them directly.
| Name | Return | Summary | |
|---|---|---|---|
$pages->deleteBranchReady() $pages->deleteBranchReady(Page $page, array $options) $pages->deleteBranchReady(Page $page, array $options) | None | Hook called before a branch of pages is about to be deleted, called on root page of branch only | |
$pages->deleteReady() $pages->deleteReady(Page $page) $pages->deleteReady(Page $page, array $options = []) | None | Hook called when a page is about to be deleted, but before data has been touched | |
$pages->deleted() $pages->deleted(Page $page) $pages->deleted(Page $page, array $options = []) | None | Hook called after a page and its data have been deleted | |
$pages->deletedBranch() $pages->deletedBranch(Page $page, array $options, int $numDeleted) $pages->deletedBranch(Page $page, array $options, int $numDeleted) | None | Hook called after a a branch of pages has been deleted, called on root page of branch only |
Status hooks
Methods called automatically when a page status changes. You can hook these methods but should not call them directly.
| Name | Return | Summary | |
|---|---|---|---|
$pages->publishReady() $pages->publishReady(Page $page) $pages->publishReady(Page $page) | None | Hook called right before an unpublished page is published and saved | |
$pages->published() $pages->published(Page $page) $pages->published(Page $page) | None | Hook called after an unpublished page has just been published | |
$pages->statusChangeReady() $pages->statusChangeReady(Page $page) $pages->statusChangeReady(Page $page) | None | Hook called when a page's status is about to be changed and saved | |
$pages->statusChanged() $pages->statusChanged(Page $page) $pages->statusChanged(Page $page) | None | Hook called when a page status has been changed and saved | |
$pages->unpublishReady() $pages->unpublishReady(Page $page) $pages->unpublishReady(Page $page) | None | Hook called right before a published page is unpublished and saved | |
$pages->unpublished() $pages->unpublished(Page $page) $pages->unpublished(Page $page) | None | Hook called after published page has just been unpublished |
Find hooks
Method called automatically when pages are found. You can hook this method but should not call it directly.
| Name | Return | Summary | |
|---|---|---|---|
$pages->found() $pages->found(PageArray $pages, array $details) $pages->found(PageArray $pages, array $details) | None | Hook called at the end of a $pages->find(), includes extra info not seen in the resulting PageArray |
Additional methods and properties
In addition to the methods and properties above, Pages also inherits the methods and properties of these classes:
API reference based on ProcessWire core version 3.0.261