The $pages API variable

$pages is your connection to all the pages in your site

While the $page variable holds the current page, the $pages variable is where you can get at all the other pages in your site. It includes functions for getting, finding, saving and deleting pages.

$pages API reference

View all $pages API methods in the API reference.

Examples of getting and finding pages

These examples correspond to the skyscrapers demo site. For this first example, we'll get a specific page and print it's title:

echo $pages->get("/cities/chicago/sears-tower/")->title; 

Find all skyscrapers (i.e. pages using the skyscraper template). Then make a list linking to each of them. Note that this foreach() example below also applies to all examples below this – the foreach() has been left out of later examples for brevity.

$skyscrapers = $pages->find("template=skyscraper");
foreach($skyscrapers as $s) {
  echo "<li><a href='$s->url'>$s->title</a></li>";
}

Find all skyscrapers with a height greater than 500 ft, and less than or equal to 1000 ft:

$skyscrapers = $pages->find("template=skyscraper, height>500, height<=1000");

Find all skyscrapers in Chicago with 60+ floors, sorted by floors ascending:

$skyscrapers = $pages->get("/cities/chicago/")->find("floors>=60, sort=floors");

Find all skyscrapers built before 1950 with 10+ floors, sorted by year descending, then floors descending:

$skyscrapers = $pages->find("template=skyscraper, year<1950, floors>=10, sort=-year, sort=-floors");

Find all skyscrapers by architects David Childs or Renzo Piano, and sort by height descending:

$david = $pages->get("/architects/david-childs/");
$renzo = $pages->get("/architects/renzo-piano/");
$skyscrapers = $pages->find("architect=$david|$renzo, sort=-height");

Find all skyscrapers that mention the words "limestone" and "granite" somewhere in their body copy:

$skyscrapers = $pages->get("/cities/")->find("template=skyscraper, body~=limestone granite");

Find all skyscrapers that mention the phrase "empire state building" in their body copy:

$pages->get("/cities/")->find("template=skyscraper, body*=empire state building");

Examples of saving and deleting pages

Load a page, change it's year field and save it:

$skyscraper = $pages->get("/cities/chicago/sears-tower/");
$skyscraper->year = 1974;
$pages->save($skyscraper); // $skyscraper->save() does the same thing

Find all pages with the text "Sears Tower" in their bodycopy, and update them to say "Willis Tower":

foreach($pages->find("body*=Sears Tower") as $p) {
  $p->body = str_replace("Sears Tower", "Willis Tower", $p->body);
  $p->save();
} 

Create a new skyscraper page in Atlanta and save it, then add an image and save again:

$skyscraper = new Page();
$skyscraper->template = $templates->get("skyscraper");
$skyscraper->parent = $pages->get("/cities/atlanta/");
$skyscraper->title = "W-Hotel Tower";
$skyscraper->height = 400;
$skyscraper->year = 2009;
$skyscraper->body = "This is a nice hotel and it has a helicopter landing pad on top of it."
$skyscraper->save();
$skyscraper->images->add("http://starwoodhotels.com/w-atlanta.jpg");
$skyscraper->save();

Find all skyscrapers built in the 1950s and move them to the trash:

$skyscrapers = $pages->find("year>=1950, year<1960");
foreach($skyscrapers as $skyscraper) {
  $pages->trash($skyscraper);
}

Permanently delete a page:

$p = $pages->get("/about/locations/southpark/");
$pages->delete($p);
// or: $pages->get("/about/locations/southpark/")->delete();

Permanently delete a page and all it's children. In this case it will delete a city page and all the skyscrapers below it:

$city = $pages->get("/cities/houston/");
$pages->delete($city, true); // specify true as a second param to make the delete recursive

Twitter updates

  • New post: ProcessWire 3.0.125 has several useful new Sanitizer methods & options, plus new ways to access them directly from the Input API variable. This makes handling user input even easier than before. Plus updates to our i18n functions & API docs— More
    25 January 2019
  • New post: In this week’s post, we’ll take a look a look at the new website and focus on some parts of it and how they were built. Then we’ll dive into the latest version of ProcessWire on the dev branch, version 3.0.124— More
    11 January 2019
  • New ProcessWire main site rebuild ready for preview— More
    4 January 2019

Latest news

  • ProcessWire Weekly #248
    In the 248th issue of ProcessWire Weekly we'll introduce a third party module called SEO Maestro, feature a handy little recipe of the week, and showcase a brilliant new site of the week called Urban Alternatives. Read on!
    Weekly.pw / 9 February 2019
  • A closer look at ProcessWire ProMailer
    This week we take a closer look at the upcoming ProMailer module with a lot more details and screenshots. Plus an update on next steps with the new website and development schedule in the weeks ahead. 
    Blog / 1 February 2019
  • Subscribe to weekly ProcessWire news

“We were really happy to build our new portfolio website on ProcessWire! We wanted something that gave us plenty of control on the back-end, without any bloat on the front end - just a nice, easy to access API for all our content that left us free to design and build however we liked.” —Castus, web design agency in Sheffield, UK