$pages->getById() method

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

Note that this method is primarily for internal use and most of the options available are specific to the needs of core methods that utilize them. All pages loaded by ProcessWire pass through this method.

Optionally specify an $options array rather than a template for argument 2. When present, the template and parent_id arguments may be provided in the given $options array. These options may be specified:

LOAD OPTIONS (argument 2 array):

  • cache (boolean): Place loaded pages in memory cache? (default=true)
  • getFromCache (boolean): Allow use of previously cached pages in memory (rather than re-loading it from DB)? (default=true)
  • template (Template): Instance of Template, see the $template argument for details.
  • parent_id (integer): Parent ID, see $parent_id argument for details.
  • getNumChildren (boolean): Specify false to disable retrieval and population of 'numChildren' Page property. (default=true)
  • getOne (boolean): Specify true to return just one Page object, rather than a PageArray. (default=false)
  • autojoin (boolean): Allow use of autojoin option? (default=true)
  • joinFields (array): Autojoin the field names specified in this array, regardless of field settings (requires autojoin=true). (default=empty)
  • joinSortfield (boolean): Whether the 'sortfield' property will be joined to the page. (default=true)
  • findTemplates (boolean): Determine which templates will be used (when no template specified) for more specific autojoins. (default=true)
  • pageClass (string): Class to instantiate Page objects with. Leave blank to determine from template. (default=auto-detect)
  • pageArrayClass (string): PageArray-derived class to store pages in (when 'getOne' is false). (default=PageArray)
  • pageArray (PageArray|null): Populate this existing PageArray rather than creating a new one. (default=null)
  • page (Page|null): Existing Page object to populate (also requires the getOne option to be true). (default=null)

Use the $options array for potential speed optimizations:

  • Specify a template with your call, when possible, so that this method doesn't have to determine it separately.
  • Specify false for getNumChildren for potential speed optimization when you know for certain pages will not have children.
  • Specify false for autojoin for potential speed optimization in certain scenarios (can also be a bottleneck, so be sure to test).
  • Specify false for joinSortfield for potential speed optimization when you know the Page will not have children or won't need to know the order.
  • Specify false for findTemplates so this method doesn't have to look them up. Potential speed optimization if you have few autojoin fields globally.
  • Note that if you specify false for findTemplates the pageClass is assumed to be 'Page' unless you specify something different for the 'pageClass' option.

Example

// Retrieve pages by IDs in CSV string
$items = $pages->getById("1111,2222,3333");

// Retrieve pages by IDs in PHP array
$items = $pages->getById([1111,2222,3333]);

// Specify that retrieved pages are using template 'skyscraper' as an optimization
$items = $pages->getById([1111,2222,3333], $templates->get('skyscraper'));

// Retrieve pages with $options array
$items = $pages->getById([1111,2222,3333], [
  'template' => $templates->get('skyscraper'),
  'parent_id' => 1024
]);

Usage

// basic usage
$items = $pages->getById($_ids);

// usage with all arguments
$items = $pages->getById($_ids, $template = null, $parent_id = null);

Arguments

NameType(s)Description
_idsarray, WireArray, string

Array of Page IDs or CSV string of Page IDs.

template (optional)Template, array, null

Specify a template to make the load faster, because it won't have to attempt to join all possible fields... just those used by the template. Optionally specify an $options array instead, see the method notes above.

parent_id (optional)int, null

Specify a parent to make the load faster, as it reduces the possibility for full table scans. This argument is ignored when an options array is supplied for the $template.

Return value

PageArray Page

Returns Page only if the 'getOne' option is specified, otherwise always returns a PageArray.

Exceptions

Method can throw exceptions on error:

  • WireException


$pages methods and properties

API reference based on ProcessWire core version 3.0.155

Twitter updates

  • This post covers a few of the bigger updates in ProcessWire 3.0.154+3.0.155. This includes new live replacement of text in core and modules, a new method for creating canonical URLs, and some major upgrades to our input->urlSegment() method! More
    24 April 2020
  • A brief look at what's new in ProcessWire 3.0.154 in this forum post: More
    17 April 2020
  • This week we’ve got a few new and interesting core updates in progress, though none quite ready to release just yet. So rather than releasing version 3.0.154 today, I thought we'd instead take a brief look at what’s coming over the next few weeks… More
    3 April 2020

Latest news

  • ProcessWire Weekly #311
    In the 311th issue of ProcessWire Weekly we're going to talk a bit about the latest dev versions of ProcessWire, introduce a couple of new third party modules, and more. Read on!
    Weekly.pw / 25 April 2020
  • ProcessWire 3.0.154 and 3.0.155 core updates
    This post covers a few of the bigger updates in ProcessWire 3.0.154 and 3.0.155 on the dev branch. This includes a new function for live replacement of text in core and modules, a new method for creating canonical URLs, and some major upgrades to our $input->urlSegment() method that I think you’ll like!
    Blog / 24 April 2020
  • 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