Get CMS Page collection by URL Key in Magento 2

You can get specific 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 PageRepositoryInterface in __construct() method of Class.  If you want to Get All CMS pages Collection refer,  Get All CMS Pages Collection Magento 2

To access Pages Collection by URL key, Filter page collection by identifier,
$searchCriteria = $this->searchCriteriaBuilder->addFilter(‘identifier’, ‘about-us’,’eq’)->create();

In above query, identifier used as page url key.

We have used equal conditions, You can set any conditions as the third parameter in the above conditions.

List of Conditions are,

‘eq’, ‘neq’, ‘like’, ‘nlike’, ‘in’, ‘nin’, ‘notnull’, ‘null’, ‘gt’,’lt’, ‘gteq’, ‘lteq’, ‘finset’

Example for like condition,   addFilter(‘identifier’,’%test%’,’like’)

I will give you demo using creating Block Class,

From Template file, you need to pass Page id and based on Page id you can get Page Collection.

Based on the above Code snippet you get all the page data from page url_key.

Leave a Reply

Your email address will not be published. Required fields are marked *