$page->preload() method

Preload multiple fields together as a group (experimental)

This is an optimization that enables you to load the values for multiple fields into a page at once, and often in a single query. For fields where it is supported, and for cases where you have a lot of fields to load at once, it can be up to 50% faster than the default of lazy-loading fields.

To use, call $page->preload([ 'field1', 'field2', 'etc.' ]) before accessing $page->field1, $page->field2, etc.

The more fields you give this method, the more performance improvement it can offer. As a result, don't bother if with only a few fields, as it's less likely to make a difference at small scale. You will also see a more measurable benefit if preloading fields for lots of pages at once.

Preload works with some Fieldtypes and not others. For details on what it is doing, specify true for the debug option which will make it return array of what it loaded and what it didn't. Have a look at this array with TracyDebugger or output a print_r() call on it, and the result is self explanatory.

NOTE: This function is currently experimental, recommended for testing only.

Available since version 3.0.243.

Example

// Example usage
$page->preload([ 'headline', 'body', 'sidebar', 'intro', 'summary' ]);
echo "
  <h1 id='headline'>$page->headline</h1>";
  <div id='intro'>$page->intro</div>
  <div id='body'>$page->body</div>
  <aside id='sidebar' pw-append>$page->sidebar</aside>
  <meta id='meta-description'>$page->summary</meta>
";

Usage

// basic usage
$array = $page->preload();

// usage with all arguments
$array = $page->preload(array $fieldNames = [], array $options = []);

Arguments

NameType(s)Description
fieldNames (optional)array

Names of fields to preload or omit (or blank array) to preload all supported fields.

options (optional)array

Options to modify default behavior:

  • debug (bool): Specify true to return additional info in returned array (default=false).
  • See the PagesLoader::preloadFields() method for additional options.

Return value

array

Array of details


$page methods and properties

API reference based on ProcessWire core version 3.0.244

Latest news

  • ProcessWire Weekly #558
    In the 558th issue of ProcessWire Weekly we'll check out the blog post that introduces ProcessWire 3.0.244, share some recent ProcessWire highlights, and more. Read on!
    Weekly.pw / 18 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“I am currently managing a ProcessWire site with 2 million+ pages. It’s admirably fast, and much, much faster than any other CMS we tested.” —Nickie, Web developer