{"id":1597,"date":"2021-02-25T10:01:10","date_gmt":"2021-02-25T10:01:10","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/get-all-cms-pages-in-system-configuration-in-magento-2\/"},"modified":"2025-07-16T12:57:04","modified_gmt":"2025-07-16T07:27:04","slug":"get-all-cms-pages-in-system-configuration-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/get-all-cms-pages-in-system-configuration-in-magento-2\/","title":{"rendered":"How to Get All CMS Pages In System Configuration in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">CMS pages are static pages that allow adding text, video, photos, etc and provide the facility to create rich content and the ease of managing multiple contents.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The default\u00a0Magento 2\u00a0CMS pages include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Home Page<\/li>\n\n\n\n<li>Category Page<\/li>\n\n\n\n<li>Product Page<\/li>\n\n\n\n<li>About Us Page<\/li>\n\n\n\n<li>Customer service Page<\/li>\n\n\n\n<li>Privacy policy<\/li>\n\n\n\n<li>404 not found<\/li>\n\n\n\n<li>Enable cookies<\/li>\n\n\n\n<li>503 service unavailable<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Moreover, the store owner can also create a custom CMS page as per requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One often has to work with CMS pages while working with&nbsp;<a href=\"https:\/\/meetanshi.com\/magento-2-extensions.html\" target=\"_blank\" rel=\"noreferrer noopener\">Magento 2 extension<\/a>&nbsp;development or customization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For instance, the admin needs to choose and exclude CMS page from a list of all CMS pages in system configuration as mentioned in the below image:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2021\/02\/Screenshot-at-January-9th-2021-1.35.59-pm@1.25x.png\" alt=\"How to Get All CMS Pages In System Configuration in Magento 2\" class=\"wp-image-13247\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In that scenario, you need to&nbsp;<strong><em>get all CMS pages in system configuration in Magento 2<\/em><\/strong>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the below code the right way and you are done!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Steps to Get All CMS Pages In System Configuration in Magento 2<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>registration.php<\/strong>&nbsp;file at&nbsp;<strong>app\/code\/Vendor\/Module<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(\n    ComponentRegistrar::MODULE,\n    'Vendor_Module',\n    __DIR__\n);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>module.xml<\/strong>&nbsp;at&nbsp;<strong>app\/code\/Vendor\/Module\/etc&nbsp;<\/strong>and use the below code.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\">\n    &lt;\/module>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">3. Create&nbsp;<strong>system.xml&nbsp;<\/strong>at&nbsp;<strong><strong>app\/code\/Vendor\/Module\/etc\/adminhtml.<\/strong><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n    &lt;system>\n        &lt;tab id=\"vendor\" translate=\"label\" sortOrder=\"100\">\n            &lt;label>vendor&lt;\/label>\n        &lt;\/tab>\n        &lt;section id=\"section_id\" translate=\"label\" type=\"text\" sortOrder=\"320\" showInDefault=\"1\" showInWebsite=\"1\"\n                 showInStore=\"1\">\n            &lt;label>Module Name&lt;\/label>\n            &lt;tab>vendor&lt;\/tab>\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\"\n                   showInWebsite=\"1\" showInStore=\"1\">\n                &lt;label>CMS Pages&lt;\/label>\n                &lt;field id=\"cms_pages\" translate=\"label\" type=\"multiselect\" sortOrder=\"50\" showInDefault=\"1\"\n                       showInWebsite=\"1\" showInStore=\"1\">\n                    &lt;label>CMS Pages&lt;\/label>\n                    &lt;source_model>\\Vendor\\Module\\Model\\Config\\Source\\CmsPages&lt;\/source_model>\n                &lt;\/field>\n            &lt;\/group>\n        &lt;\/section>\n    &lt;\/system>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">4. Create&nbsp;<strong>CmsPages.php<\/strong>&nbsp;file at&nbsp;<strong>app\/code\/Vendor\/Module\/Model\/Config\/Source<\/strong>.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Model\\Config\\Source;\n\nuse Magento\\Cms\\Api\\Data\\PageInterface;\nuse Magento\\Cms\\Api\\PageRepositoryInterface;\nuse Magento\\Framework\\Api\\SearchCriteriaBuilder;\nuse Magento\\Framework\\App\\ObjectManager;\nuse Magento\\Framework\\Exception\\LocalizedException;\nuse Magento\\Framework\\Option\\ArrayInterface;\nuse Psr\\Log\\LoggerInterface;\n\n\/**\n * Class CmsPages\n * @package Vendor\\Module\\Model\\Config\\Source\n *\/\nclass CmsPages implements ArrayInterface\n{\n    \/**\n     * @var PageRepositoryInterface\n     *\/\n    private $pageRepositoryInterface;\n\n    \/**\n     * @var SearchCriteriaBuilder\n     *\/\n    private $searchCriteriaBuilder;\n\n    \/**\n     * CmsPages constructor.\n     * @param PageRepositoryInterface $pageRepositoryInterface\n     * @param SearchCriteriaBuilder $searchCriteriaBuilder\n     *\/\n    public function __construct(\n        PageRepositoryInterface $pageRepositoryInterface,\n        SearchCriteriaBuilder $searchCriteriaBuilder\n    )\n    {\n        $this->pageRepositoryInterface = $pageRepositoryInterface;\n        $this->searchCriteriaBuilder = $searchCriteriaBuilder;\n    }\n\n    \/**\n     * @return array\n     *\/\n    public function toOptionArray()\n    {\n        $optionArray = [];\n        try {\n            $pages = $this->getCmsPageCollection();\n            if ($pages instanceof LocalizedException) {\n                throw $pages;\n            }\n            $cnt = 0;\n            foreach ($pages as $page) {\n                $optionArray[$cnt]['value'] = $page->getIdentifier();\n                $optionArray[$cnt++]['label'] = $page->getTitle();\n            }\n        } catch (LocalizedException $e) {\n            ObjectManager::getInstance()->get(LoggerInterface::class)->info($e->getMessage());\n        } catch (\\Exception $e) {\n            ObjectManager::getInstance()->get(LoggerInterface::class)->info($e->getMessage());\n        }\n        return $optionArray;\n    }\n\n    \/**\n     * @return \\Exception|PageInterface[]|LocalizedException\n     *\/\n    public function getCmsPageCollection()\n    {\n        $searchCriteria = $searchCriteria = $this->searchCriteriaBuilder->create();\n        try {\n            $collection = $this->pageRepositoryInterface->getList($searchCriteria)->getItems();\n        } catch (LocalizedException $e) {\n            return $e;\n        }\n        return $collection;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it. Make your extensions work expectedly by certain configurations that are made to have particular values for that you need to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/set-default-values-for-magento-2-system-configuration\/\">set Magento 2 default value system configuration<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Feel free to share the above solution with fellow developers via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>CMS pages are static pages that allow adding text, video, photos, etc and provide the facility to create rich content and the ease of managing multiple contents. The default\u00a0Magento 2\u00a0CMS pages include: Moreover, the store owner can also create a custom CMS page as per requirements. One often has to work with CMS pages while [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1597","post","type-post","status-publish","format-standard","hentry","category-magento"],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1597","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1597"}],"version-history":[{"count":3,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1597\/revisions"}],"predecessor-version":[{"id":14476,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1597\/revisions\/14476"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1597"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1597"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1597"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}