Get CMS Block content by identifier in Magento 2.

You can get the CMS Static Blocks collection by an identifier in Magento 2 by calling the BlockRepositoryInterface interface.

Magento\Cms\Api\BlockRepositoryInterface is used for getting CMS Static Blocks in Magento 2.

BlockRepositoryInterface is used when you need to fetch a collection of CMS Static blocks or get specific CMS static block data.

You need to instantiate BlockRepositoryInterface in __construct() method of Class.

I will give you a demo using Block Class,

Get CMS Static Block by identifier, You need to pass the identifier in the searchCriteriaBuilder addFilter() method,
Let’s assume the CMS Static Block identifier is footer_links_block,

$searchCriteria = $this->searchCriteriaBuilder->addFilter(‘identifier’, ‘footer_links_block’,’eq’)->create();
We have used equal conditions, You can set any conditions as a third parameter in the above function as per your needs.

The list of Conditions is from the given list,

        • ‘eq’
        • ‘neq’
        • ‘like’
        • ‘nlike’,
        • ‘in’,
        • ‘nin’
        • ‘notnull’
        • ‘null’
        • ‘gt’
        • ‘lt’
        • ‘gteq’
        • ‘lteq’,
        • ‘finset’
<?php
declare(strict_types=1);

namespace Rbj\CmsBlock\Model;

use Magento\Cms\Api\BlockRepositoryInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;

class CmsBlock
{
    public function __construct(
        private readonly BlockRepositoryInterface $blockRepository,
        private readonly SearchCriteriaBuilder $searchCriteriaBuilder
    ) {
    }

    /* Get Cms Blocks Collection from store. */
    public function getCmsBlock()
    {
        $searchCriteria = $this->searchCriteriaBuilder->addFilter('identifier', 'footer_links_block', 'eq')->create();
        $cmsBlock = $this->blockRepository->getList($searchCriteria)->getItems();
        return $cmsBlock;
    }
}

From the Template file, You can access all the Static Blocks by iterating loop over a collection.

Based on the above Code snippet you get all the CMS Static Blocks from the store. You can also get specific Static Blocks based on your custom condition by referring to the link.