The Pages class allows loading and manipulating page objects. The Pages class also inherits all the methods and properties of Module.

Returns the first page matching the given selector

If you need a specific page, use Pages::getPage() method:

use Scriptor\Core\Scriptor;

$pages = Scriptor::getSite()->pages();
$page = $pages->getPage('slug=my-page');

If you want to search for a page in an array, pass the array as the second parameter:

$page = $site->pages()->getPage('name=My %', $pagesArray);

… it searches for a page with the name My page, My document or My whatever etc.

Supported selector operators:

=          Equal to  
%word      Contains the exact word or phrase at the beginning of the field value.
word%      Contains the exact word or phrase at the end of the field value.
%word%     Contains the exact word or phrase.

Here are the page fields and attributes you can access with this method, but also with many others, below.

Select multiple pages

The Pages::getPages() method is useful when you want to select more than one page. To select multiple pages based on a selector string, the matching objects are returned in an array:

$pages = Scriptor::getSite()->pages()->getPages('your_field_name=value');

The selector operators that can be used:

=          Equal to.
!=         Not equal to.   
<          Less than.   
>          Greater than.   
<=         Less than or equal to.
>=         Greater than or equal to.
%word      Contains the exact word or phrase at the beginning of the field value.
word%      Contains the exact word or phrase at the end of the field value.
%word%     Contains the exact word or phrase.

For example, retrieve all pages that do not have a hidden template assigned to them:

$pages = Scriptor::getSite()->pages();
$visible = $pages->getPages('template!=hidden');

Another example, select all pages that were created later than a specific page:

$pages = Scriptor::getSite()->pages();
$page = $pages->getPage('name=Specific Page');
$createdAfter = $pages->getPages("created>$page->created");

Find all pages that have been updated after October 7, 2022:

$date = new DateTimeImmutable('2022-10-07');
$pages = $site->pages()->getPages('updated>'.$date->getTimestamp());

You can pass options as the second parameter for getPages():

$obtions = [
    'all' => false, // set it to true if you want to select the inactive pages also.
    'sortBy' => 'position', // sort by attribute or field name.
    'order' => 'asc', // sorting in ascending or descending order.
    'offset' => 0, // used to specify the number of records to return.
    'length' => 0, //used to specify the number of records to return.
    'items' => [] // An array of pages, leave it empty if you want to search all pages.
];

$pages = $site->pages()->getPages('template!=hidden', $obtions);

Create new page

If you want to add a new page, create a new Page object:

use Scriptor\Core\Page;

$page = new Page();
$page->set('name', 'My page')
        ->set('content', 'This is the page content ...')
        ->set('active', true)
        ->save();

Delete Pages

Permanently delete a page and its assets:

$site->pages()->deletePage($page, $options);

Where $page is a Page (object), or page ID (integer) to be deleted.

The $options is an array with settings to change the behavior. If you attempt to delete a page with children and don't explicitly set the recursive argument to true, the recursive delete fails for some reason. Other options that are available: clearCache whether to clear markup cache after delete (default=true). And one more option is force. You can set it to true if you want to perform the deletion despite the existence of the child pages (default=false).

The return value of the method is a bool that returns true (success) and false (failure).

The method can throw \ErrorException on fatal error.


Let's say you want to delete a page My Page that has multiple child pages. Normally, with Pages::deletePage($page) only pages without child pages are deleted, and the deletion fails:

$page = $site->pages()->getPage('name=My Page');
$result = $site->pages()->deletePage($page);

… and you get a Fatal error: Uncaught ErrorException: The operation was not approved. in ...

You may also find more information in variable Site::$notes, this contains an array of notifications and this is how you can retrieve them:

try {
    $result = $site->pages()->deletePage($page);
} catch (\ErrorException $e) {
    var_dump('Exception: ' . $e->getMessage());  // exception message
    var_dump($site->getNotes());    // deletion notes.
}

To delete the page after all, you could use the recursive option. However, note that all child pages will be deleted as a result:

$result = $site->pages()->deletePage($page, ['recursive' => true]);

Alternatively, if you want to delete only the one single page, regardless of the existence of child pages, use the force option:

$result = $site->pages()->deletePage($page, ['force' => true]);

If you don't want to clear the cache every time a page is deleted, set the option to false:

$options = [
    'clearCache' => false
];

$result = $site->pages()->deletePage($page, $options);