Quite often it is required to get data by a field value. In our example I will use nodes, but it can be any entity type.
In first example we load entities by based fields. For example vid, type, language and etc.
I want to get all the nodes of the type post for my custom block. For this we will use EntityTypeManager class
// Load a storage for node entity type $items = \Drupal::entityTypeManager() ->getStorage('node') ->loadByProperties(['type' => 'post']); // load all nodes with the node type
After we can run through the items and apply our custom logic here.
In the second example, we will get entities based on custom fields. For example we have field "field_type"
$query = $this->entityTypeManager->getStorage('node')->getQuery(); $query->condition('status', 1) ->condition('changed', REQUEST_TIME, '<') ->condition('field_type', 'post'); $nids = $query->execute();
More examples can be found here: EntityFieldQuery