How to get all images from entries including Matrix field I have a photo gallery that pulls in all entries and then finds the images within them and then displays them along with links to the entries that used that image.
Except, it's not pulling in the ones that are in a matrix field named extendedContent with a block type of insertedImage and a asset field named image.
A simplified version of the code I'm using is:

{% set content = craft.entries({
    section: 'news,information,events',
    with: ['imageGallery','extendedContent.insertedImage:image']
}) %}

{% paginate craft.assets.relatedTo(content).order('elements.dateCreated desc').kind('image').limit(25) as pageInfo, usedPhotos %}

{# Loop through the array of assets #}
{% for imageGallery in usedPhotos %}  
    &lt;img src="{{ imageGallery.url }}"&gt;

    {# find all entries related to this asset #}
    {% set entry = craft.entries.relatedTo(imageGallery).order('title') %}
        {% for entry in entry %}
            {{ entry.title }}
        {% endfor %}  

{% endfor %}


Added: I'm guessing the issue is that the relationship of the image in the matrix field isn't to the entry but to the block it's in?
Does that mean that it's not possible to do what I want?
I doubt there is a good solution in twig but you can fetch the entries with php.

First you have to get all used asset Ids. So you'll check in the relations table for all used assets with your 2 fields and for entries with your section ids



public function getAllAssetsRelatedToEntries(){
    // list the ids of the sections, so you need the id of news, information and events
    $sectionIds = [2, 7];
    // id of your insertedImage field
    $fieldIdMatrixContent = 6;
    // id of your imageGallery field
    $fieldIdImageGallery = 7;

    // fetches all assets that have a relation in the matrix field
    $imagesFromMatrixContent =  craft()-&gt;db-&gt;createCommand()
        -&gt;select('craft_relations.targetId')
        -&gt;from('relations')
        -&gt;join('matrixblocks', 'craft_matrixblocks.id = craft_relations.sourceId')
        -&gt;join('entries', 'craft_entries.id = craft_matrixblocks.ownerId')
        -&gt;where('craft_relations.fieldId = :fieldId', ['fieldId' =&gt; $fieldIdMatrixContent])
        -&gt;andWhere(['in', 'craft_entries.sectionId', $sectionIds])
        -&gt;queryColumn();

    // fetches all assets that are related with the ImageGallery Field
    $imagesFromNormalField = craft()-&gt;db-&gt;createCommand()
        -&gt;select('craft_relations.targetId')
        -&gt;from('relations')
        -&gt;where('craft_relations.fieldId = :fieldId', ['fieldId' =&gt; $fieldIdImageGallery])
        -&gt;join('entries', 'craft_entries.id = craft_relations.sourceId')
        -&gt;andWhere(['in', 'craft_entries.sectionId', $sectionIds])
        -&gt;queryColumn();

    return array_unique(array_merge($imagesFromMatrixContent,$imagesFromNormalField));
}


This will return you an array of all ids, thus you can build your paginate or other queries like you want and change the sort order or include other conditions

{% set allAssetIds = craft.pFplugin.getAllAssetsRelatedToEntries() %}
{% paginate craft.assets.id(allAssetIds).kind('image').limit('30') as pageInfo, usedPhotos %}


The next step will be to search the entries for each asset


public function getAllEntriesForAsset($assetId)
{
    // list the ids of the sections, so you need the id of news, information and events
    $sectionIds = [2, 7];
    // id of your insertedImage field
    $fieldIdMatrixContent = 6;
    // id of your imageGallery field
    $fieldIdImageGallery = 7;

    $entryIdsFromMatrix = craft()-&gt;db-&gt;createCommand()
        -&gt;select('craft_matrixblocks.ownerId')
        -&gt;from('relations')
        -&gt;join('matrixblocks', 'craft_matrixblocks.id = craft_relations.sourceId')
        -&gt;where('craft_relations.targetId = :assetId', ['assetId' =&gt; $assetId])
        -&gt;join('entries', 'craft_entries.id = craft_matrixblocks.ownerId')
        -&gt;andWhere('craft_relations.fieldId = :fieldId', ['fieldId' =&gt; $fieldIdMatrixContent])
        -&gt;andWhere(['in', 'craft_entries.sectionId', $sectionIds])
        -&gt;queryColumn();

    $entryIdsImageGallery = craft()-&gt;db-&gt;createCommand()
        -&gt;select('craft_relations.sourceId')
        -&gt;from('relations')
        -&gt;where('craft_relations.targetId = :assetId', ['assetId' =&gt; $assetId])
        -&gt;join('entries', 'craft_entries.id = craft_relations.sourceId')
        -&gt;andWhere(['in', 'craft_entries.sectionId', $sectionIds])
        -&gt;andWhere('craft_relations.fieldId = :fieldId', ['fieldId' =&gt; $fieldIdImageGallery])
        -&gt;queryColumn();

    return array_unique(array_merge($entryIdsFromMatrix,$entryIdsImageGallery));
}


In your template


{% set entriesForAsset = craft.pFplugin.getAllEntriesForAsset(asset.id) %}
{% for entry in craft.entries().id(entriesForAsset).order('title') %}
    {{ entry.id }} {{ entry.title }}
{% endfor %}


In total it could look like this I just did not use the paginate


{% set allAssetIds = craft.pFplugin.getAllAssetsRelatedToEntries() %}
{% for asset in craft.entries.id(allAssetIds).find() %}
    {{ asset.id }} {{ asset.title }}
    Entries for Image&lt;br&gt;
    {% set entriesForAsset = craft.pFplugin.getAllEntriesForAsset(asset.id) %}
        {% for entry in craft.entries().id(entriesForAsset).order('title') %}
        {{ entry.id }} {{ entry.title }}
    {% endfor %}
{% endfor %}