$pages->find() method

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() or Pages::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

NameType(s)Description
selectorstring, 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.

  • findOne (boolean): Apply optimizations for finding a single page (default=false).
  • findAll (boolean): Find all pages with no exclusions, same as "include=all" option (default=false).
  • findIDs (boolean|int): Specify 1 to return array of only page IDs, or true to return verbose array (default=false).
  • getTotal (boolean): Whether to set returning PageArray's "total" property (default=true, except when findOne=true).
  • loadPages (boolean): Whether to populate the returned PageArray with found pages (default=true). The only reason why you'd want to change this to false would be if you only needed the count details from the PageArray: getTotal(), getStart(), getLimit, etc. This is intended as an optimization for $pages->count(). Does not apply if $selector argument is an array.
  • cache (boolean): Allow caching of selectors and loaded pages? (default=true). Also sets loadOptions[cache].
  • allowCustom (boolean): Allow use of _custom="another selector" in given $selector? For specific uses. (default=false)
  • caller (string): Optional name of calling function, for debugging purposes, i.e. "pages.count" (default=blank).
  • include (string): Optional inclusion mode of 'hidden', 'unpublished' or 'all'. (default=none). Typically you would specify this directly in the selector string, so the option is mainly useful if your first argument is not a string.
  • stopBeforeID (int): Stop loading pages once page matching this ID is found (default=0).
  • startAfterID (int): Start loading pages once page matching this ID is found (default=0).
  • lazy (bool): Specify true to force lazy loading. This is the same as using the Pages::findMany() method (default=false).
  • loadOptions (array): Optional associative array of options to pass to getById() load options.

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->find(…) 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->find(…) 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->find(…) 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


$pages methods and properties

API reference based on ProcessWire core version 3.0.137

Twitter updates

  • We added WebP image support this year and many have been adopting it in existing projects. We look at a process used for migrating existing websites to WebP images— from preliminary considerations to implementation & testing, w/lots of tips & tricks too: More
    16 August 2019
  • Core version 3.0.137 on the dev branch adds the ability to hook multiple methods at once, in a single call. This post details how it works and provides a useful example of how you might put it to use in your development environment— More
    2 August 2019
  • ProcessWire 3.0.136 updates our CKEditor version and adds a new backtrace() method to the core Debug class— More
    26 July 2019

Latest news

  • ProcessWire Weekly #275
    In the 275th issue of ProcessWire Weekly we're going to walk you through the new features included in ProcessWire 3.0.138, cover some WebP related stuff, and highlight recent forum posts and online resources. Read on!
    Weekly.pw / 17 August 2019
  • WebP images on an existing site
    In this post we’ll look at a process used for migrating an existing website to use WebP images. We’ll cover everything from preliminary considerations to implementation and testing, with lots of tips and tricks along the way.
    Blog / 16 August 2019
  • Subscribe to weekly ProcessWire news

“To Drupal, or to ProcessWire? The million dollar choice. We decided to make an early switch to PW. And in retrospect, ProcessWire was probably the best decision we made. Thanks are due to ProcessWire and the amazing system and set of modules that are in place.” —Unni Krishnan, Founder of PigtailPundits