Given a Selector string, return the Page objects that match in a PageArray.
- This is one of the most commonly used API methods in ProcessWire.
- If you only need to find one page, use the
Pages::get()
orPages::findOne()
method instead (and note the difference). - If you need to find a huge quantity of pages (like thousands) without limit or pagination, look at the
Pages::findMany()
method.
Example
// Find all pages using template "building" with 25 or more floors
$skyscrapers = $pages->find("template=building, floors>=25");
Usage
// basic usage
$items = $pages->find($selector);
// usage with all arguments
$items = $pages->find($selector, $options = []);
Arguments
Name | Type(s) | Description |
---|---|---|
selector | string, int, array, Selectors | Specify selector (standard usage), but can also accept page ID or array of page IDs. |
options (optional) | array, string | One or more options that can modify certain behaviors. May be associative array or "key=value" selector string.
|
Return value
PageArray
array
PageArray of that matched the given selector, or array of page IDs (if using findIDs option).
Non-visible pages are excluded unless an "include=x" mode is specified in the selector (where "x" is "hidden", "unpublished" or "all"). If "all" is specified, then non-accessible pages (via access control) can also be included.
Hooking $pages->find(…)
You can add your own hook events that are executed either before or after the $pages
method is executed. Examples of both are included below. A good place for hook code such as this is in your /site/ready.php file.
Hooking before
The 'before' hooks are called immediately before each $pages
method call is executed. This type of hook is especially useful for modifying arguments before they are sent to the method.
$this->addHookBefore('Pages::find', function(HookEvent $event) {
// Get the object the event occurred on, if needed
$pages = $event->object;
// Get values of arguments sent to hook (and optionally modify them)
$selector = $event->arguments(0);
$options = $event->arguments(1);
/* Your code here, perhaps modifying arguments */
// Populate back arguments (if you have modified them)
$event->arguments(0, $selector);
$event->arguments(1, $options);
});
Hooking after
The 'after' hooks are called immediately after each $pages
method call is executed. This type of hook is especially useful for modifying the value that was returned by the method call.
$this->addHookAfter('Pages::find', function(HookEvent $event) {
// Get the object the event occurred on, if needed
$pages = $event->object;
// An 'after' hook can retrieve and/or modify the return value
$return = $event->return;
// Get values of arguments sent to hook (if needed)
$selector = $event->arguments(0);
$options = $event->arguments(1);
/* Your code here, perhaps modifying the return value */
// Populate back return value, if you have modified it
$event->return = $return;
});
See Also
API reference based on ProcessWire core version 3.0.236