Pages

The $pages API variable enables loading and manipulation of Page objects, to and from the database.

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 findind 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.252