Basic guide to using the processwire $pages variable

Posted 2nd Oct 2017

#api #pagesvariable #selector

We're going to look at the processwire $pages variable which is used to find other pages on your site.

Let's say you want to find 5 pages on your site, that were created using a 'blog-entry' template, not including a page with an id of 1001:

  // returns a PageArray with 5 blog post page objects
  $blogPosts = $pages->find("template=blog-entry, limit=5, id!=1001");

This will return a PageArray which contains the page objects themselves. A PageArray is like a regular PHP array but has many included methods for things like sorting, slicing, counting, shifting etc. For example:

  // slice the PageArray from 5 to 3
  $blogPosts = $blogPosts->slice(0, 3);

  // shuffle the order of the posts
  $blogPosts = $blogPosts->shuffle();
  // return the number of pages in the PageArray
  $numberOfPosts = $blogPosts->count();

You can iterate over the PageArray (presuming here that each blog post has a 'summary' field, title is mandatory:

<h1>Showing <?php echo $numberOfPosts; ?> blog posts</h1>
<?php foreach ($blogPosts as $post): ?>
<div class="row">
  <h1><?php echo $post->title; ?></h1>
  <p><?php echo $post->summary; ?></p>
<?php endforeach; ?>

Outputs (remember we shuffled them):

<h1>Showing 3 blog posts</h1>

<div class="row">
  <h1>Blog post 1</h1>
  <p>Summary for blog post 1</p>

<div class="row">
  <h1>Blog post 3</h1>
  <p>Summary for blog post 3</p>

<div class="row">
  <h1>Blog post 2</h1>
  <p>Summary for blog post 2</p>

$pages->find() vs $pages->get()

The difference here is that $pages->find() returns a PageArray and $pages->get() returns a single page or a nullpage (if nothing found).


  // return the page object by path
  $homepage = $pages->get("/home/");
  echo $homePage; // 1

  // return the page object by the page ID
  $homepage = $pages->get(1);
  echo $homepage; // 1

  // return the page object by a filter
  $pageArrayHomepage = $pages->find("template=home");
  echo $pageArrayHomepage; // no output because...

  // ...$pages->find() returns a PageArray
  // and needs to be iterated
  foreach ($pageArrayHomepage as $page) {
    echo $page; // 1

  // you can chain methods to get children of specific page
  // get homepage page object, then return PageArray
  // of this pages' children
  $homePageChildren = $pages->get(1)->children;
  echo $homePageChildren; // 1015|1016|1018


As you can see above, the API allows you to easily find pages on your site. From here, you should read these two pages and try out some of the built in methods.

Get familiar with these two concepts and you'll be well on your way!

Feedback & support

I hope you enjoyed this tutorial. You can support pwtuts by following on @pwtuts.

Related tutorials / See all