You can get all the CMS page collection in Magento 2 by calling interface, Magento\Cms\Api\PageRepositoryInterface.
PageRepositoryInterface is used when you need to fetch a collection of CMS page or get specific CMS page data.
You need to instantiate Interface in __construct() method of Class.
I will give you a demo using Block Class,
<?php
namespace Rbj\Pages\Block;
class Pages extends \Magento\Framework\View\Element\Template
{
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
\Magento\Cms\Api\PageRepositoryInterface $pageRepositoryInterface,
\Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
array $data = []
) {
$this->pageRepositoryInterface = $pageRepositoryInterface;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
parent::__construct($context, $data);
}
/* Get Pages Collection from site. */
public function getPages() {
$searchCriteria = $searchCriteria = $this->searchCriteriaBuilder->create();
$pages = $this->pageRepositoryInterface->getList($searchCriteria)->getItems();
return $pages;
}
From Template file, You need to call a function from Block,
<?php
$pages = $block->getPages();
foreach($pages as $page) {
echo "<pre>";print_r($page->getData());
echo $page->getId(); // get Id
echo $page->getTitle(); // get Title/name of CMS Page
}
?>
Based on above Code snippet you get all the pages from a website. You can also get specific pages based on your custom condition.

One Reply to “Get CMS Page Collection in Magento 2”