PagesLoader::getById() method

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

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

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.

Usage

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

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

Arguments

NameType(s)Description
_idsarray, WireArray, string, int

Array of page IDs, comma or pipe-separated string of IDs, or single page ID (string or int) or in 3.0.156+ array of associative arrays where each in format: [ 'id' => 123, 'templates_id' => 456 ]

template (optional)Template, array, string, int, 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 NullPage

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

Exceptions

Method can throw exceptions on error:

  • WireException


PagesLoader methods and properties

API reference based on ProcessWire core version 3.0.236

Latest news

  • ProcessWire Weekly #550
    In the 550th issue of ProcessWire Weekly we're going to check out the latest core updates, introduce a new third party module called PagefileMetadata, and more. Read on!
    Weekly.pw / 23 November 2024
  • Custom Fields Module
    This week we look at a new ProFields module named Custom Fields. This module provides a way to rapidly build out ProcessWire fields that contain any number of subfields/properties within them.
    Blog / 30 August 2024
  • 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