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.

Finding pages
// 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 control

find() modifiers in selector

Common selector modifiers for find(), findOne(), get(), and related methods:

OptionDefaultDescription
include=statusStatus may be hidden, unpublished, or all — include non-visible pages
limit=n0Max pages to return or 0 for no limit (also enables $items->getTotal() count)
start=n0Pagination offset/start value
sort=nameField name to sort by, prefix name with - to reverse
join=fieldsFields 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);
Creating pages
// 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.

Saving pages
$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

OptionDefaultDescription
uncacheAlltrueClear memory cache after save
resetTrackChangestrueReset page's change tracking
quietfalseSkip updating modified date/user
adjustNametrueAuto-adjust name to be unique within parent
ignoreFamilyfalseBypass family/parent restriction checks
noHooksfalseSkip before/after save hooks
noFieldsfalseSave only native page properties, not fields
Deleting and trashing pages
// 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 children
Cache

ProcessWire 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);
Creating page/array instances
$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 instance
Hooks

Useful hooks on $pages:

HookWhen fired
Pages::foundAfter find completes, receives the full PageArray
Pages::saveReadyJust before a page is saved
Pages::savedAfter a page is successfully saved
Pages::savePageOrFieldReadyBefore a page or a field is saved
Pages::savedPageOrFieldAfter a page or a field is saved
Pages::addReadyBefore a new page is added
Pages::addedAfter a new page is added
Pages::deleteReadyBefore a page is deleted
Pages::deletedAfter a page is deleted
Pages::trashReadyBefore a page is trashed
Pages::trashedAfter a page is trashed
Pages::restoreReadyBefore a page is restored from trash
Pages::restoredAfter a page is restored from trash
Pages::cloneReadyBefore a page is cloned
Pages::clonedAfter 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) {
    // ...
}); 
Helper classes (wire/core/Pages/)

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.

PropertyClassPurpose
$pages->loaderPagesLoaderAll find/get/load operations
$pages->editorPagesEditorsave, add, delete, clone
$pages->cacherPagesLoaderCacheIn-memory page cache
$pages->trasherPagesTrashtrash, restore, emptyTrash
$pages->namesPagesNamesPage name generation and uniqueness
$pages->rawPagesRawfindRaw / getRaw
$pages->pathFinderPagesPathFinderPath-to-page resolution
$pages->requestPagesRequestCurrent HTTP request page resolution
$pages->parentsPagesParentsAncestor/parent tree queries
$pages->porterPagesExportImportImport/export
Notes
  • $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 $pages write methods (save, add, delete, trash, etc.) are hookable via the triple-underscore ___methodName() pattern.
API reference: methods, hooks

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.

Show $var?     Show args?       Only hookable?    

Retrieve

NameReturnSummary 
$pages->count()
int

Count and return how many pages will match the given selector.

 
$pages->find($selector)
PageArray array

Given a Selector string, return the Page objects that match in a PageArray.

$pages->findIDs($selector)
array

Like find() except returns array of IDs rather than Page objects

 
$pages->findMany($selector)
PageArray

Like find(), but with “lazy loading” to support giant result sets without running out of memory.

 
$pages->findOne($selector)
Page NullPage

Like find() but returns only the first match as a Page object (not PageArray).

 
$pages->findRaw($selector)
array

Find pages and return raw data from them in a PHP array

 
$pages->get($selector)
Page NullPage

Returns the first page matching the given selector with no exclusions

 
$pages->getByIDs($ids)
PageArray Page

Given array or CSV string of Page IDs, return a PageArray

 
$pages->getFresh($selectorOrPage)
Page NullPage

Get a fresh, non-cached copy of a Page from the database

 
$pages->getRaw($selector)
array

Get single page and return raw data in an associative array

 
$pages->has($selector)
array int

Is there any page that matches the given $selector in the system? (with no exclusions)

 

Move

NameReturnSummary 
$pages->insertAfter(Page $page, Page $afterPage)
None

Sort/move one page after another (for manually sorted pages)

$pages->insertBefore(Page $page, Page $beforePage)
None

Sort/move one page above another (for manually sorted pages)

$pages->sort(Page $page)
int

Set the “sort” value for given $page while adjusting siblings, or re-build sort for its children

Advanced

NameReturnSummary 
$pages->findJoin($selector, $joinFields)
PageArray

Find pages and specify which fields to join (overriding configured autojoin settings)

 
$pages->getById($_ids)
PageArray Page

Given an array or CSV string of Page IDs, return a PageArray (internal API)

 
$pages->getByPath(string $path)
Page int

Get a page by its path, similar to $pages->get('/path/to/page/') but with more options

 
$pages->getID($selector)
int array

Get one ID of page matching given selector with no exclusions, like get() but returns ID rather than a Page

 
$pages->getInfoByPath(string $path)
array

Get verbose array of info about a given page path

 
$pages->getOneById(int $id)
Page NullPage

Get one page by ID

 
$pages->getPath($id)
string

Given an ID, return a path to a page, without loading the actual page

 
$pages->of()
bool

Get or set the current output formatting state

 

Cache

NameReturnSummary 
$pages->getCache()
Page array null

Given a Page ID, return it if it's cached, or NULL of it's not.

 
$pages->uncache()
int

Remove the given page(s) from the cache, or uncache all by omitting $page argument

 
$pages->uncacheAll()
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.

NameReturnSummary 
$pages->cacher()
PagesLoaderCache

Get PagesLoaderCache instance which provides methods for caching pages in memory

 
$pages->editor()
PagesEditor

Get PagesEditor instance which provides methods for saving pages to the database

 
$pages->loader()
PagesLoader

Get PagesLoader instance which provides methods for finding and loading pages

 
$pages->names()
PagesNames

Get PagesNames instance which provides API methods specific to generating and modifying page names

 
$pages->parents()
PagesParents

Get PagesParents instance which provides methods for managing parent/child relationships in the pages_parents table

 
$pages->pathFinder()
PagesPathFinder

Get the PagesPathFinder instance which provides methods for finding pages by paths

 
$pages->porter()
PagesExportImport

Get new instance of PagesExportImport for exporting and importing pages

 
$pages->raw()
PagesRaw

Get the PagesRaw instance which provides methods for finding and loading raw pages data

 
$pages->request()
PagesRequest

Get the PagesRequest instance which provides methods for identifying and loading page from current request URL

 
$pages->trasher()
PagesTrash

Get PagesTrash instance which provides methods for managing the Pages trash

 

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.

NameReturnSummary 
$pages->addReady(Page $page)
None

Hook called when a new page is about to be added and saved to the database

$pages->added(Page $page)
None

Hook called after a new page has been added

$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(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.

NameReturnSummary 
$pages->moveReady(Page $page)
None

Hook called when a page is about to be moved to another parent

$pages->moved(Page $page)
None

Hook called when a page has been moved from one parent to another

$pages->renameReady(Page $page)
None

Hook called when a page is about to be renamed i.e. had its name field change)

$pages->renamed(Page $page)
None

Hook called when a page has been renamed (i.e. had its name field change)

$pages->sorted(Page $page)
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.

NameReturnSummary 
$pages->restoreReady(Page $page)
None

Hook called when a page is about to be moved OUT of the trash (restored)

$pages->restored(Page $page)
None

Hook called when a page has been moved OUT of the trash (restored)

$pages->trashReady(Page $page)
None

Hook called when a Page is about to be trashed

$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.

NameReturnSummary 
$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(Page $page)
None

Hook called when a page is about to be deleted, but before data has been touched

$pages->deleted(Page $page)
None

Hook called after a page and its data have been deleted

$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.

NameReturnSummary 
$pages->publishReady(Page $page)
None

Hook called right before an unpublished page is published and saved

$pages->published(Page $page)
None

Hook called after an unpublished page has just been published

$pages->statusChangeReady(Page $page)
None

Hook called when a page's status is about to be changed and saved

$pages->statusChanged(Page $page)
None

Hook called when a page status has been changed and saved

$pages->unpublishReady(Page $page)
None

Hook called right before a published page is unpublished and saved

$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.

NameReturnSummary 
$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