Docs MediaHub Version 1.x Querying Assets

Querying Assets

Using ProcessWire selectors to find assets in the library by type, tag, collection, title, or any combination. Plus the Collection page class API for working with folders and hierarchies.

All MediaHub assets are standard ProcessWire pages using the template pkd-mediahub-asset. You can find them with $pages->find() selectors, just like any other pages. Collections also have their own page class with query methods built in.

Finding assets with selectors

By file type

// All image assets
$images = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_mime^=image/');

// All PDF documents
$pdfs = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_mime=application/pdf');

// All video assets
$videos = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_mime^=video/');

// All audio assets
$audio = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_mime^=audio/');

By tag

// Assets tagged "landscape"
$landscapes = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_tags.title=landscape');

// Assets with any of several tags
$results = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_tags.title=landscape|portrait|aerial');

By collection

// Assets in a collection named "branding"
$branding = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_collections.name=branding');

// Assets in a collection by ID
$collectionId = 1234;
$assets = $pages->find("template=pkd-mediahub-asset, pkd_mediahub_collections=$collectionId");

By title or search

// Exact title match
$asset = $pages->get('template=pkd-mediahub-asset, title=Office Team Photo');

// Title contains
$results = $pages->find('template=pkd-mediahub-asset, title%=office');

// Search across title and alt text
$results = $pages->find('template=pkd-mediahub-asset, title|pkd_mediahub_alt%=sunset');

Sorting and limiting

// Newest first, limited to 10
$recent = $pages->find('template=pkd-mediahub-asset, sort=-created, limit=10');

// Alphabetical by title
$sorted = $pages->find('template=pkd-mediahub-asset, sort=title');

// Count without loading pages
$total = $pages->count('template=pkd-mediahub-asset');

Using the asset type methods

Once you have asset pages from a selector query, you can use the same PkdMediahubAssetPage methods described in the Template API:

$images = $pages->find('template=pkd-mediahub-asset, pkd_mediahub_mime^=image/, sort=-created, limit=20');

foreach ($images as $asset) {
    echo "<img src='{$asset->image()->width(300)->url}' alt='{$asset->altText()}'>";
}

Querying collections

Collections are pages with their own custom page class (PkdMediahubCollectionPage). You can find them with selectors or use the built-in methods.

Finding collections

// Get a collection by name
$branding = $pages->get('template=pkd-mediahub-collection, name=branding');

// All root-level collections
$rootCollections = $pages->find('template=pkd-mediahub-collection, parent.template=pkd-mediahub-collections, sort=sort');

Collection methods

MethodReturnsDescription
$collection->assets()PageArrayDirect assets in this collection only.
$collection->assets(10)PageArrayDirect assets, limited to 10.
$collection->allAssets()PageArrayAssets in this collection and all sub-collections.
$collection->assetCount()intCount of direct assets.
$collection->allAssetCount()intCount including sub-collections.
$collection->subCollections()PageArrayDirect child collections.
$collection->hasSubCollections()boolWhether this collection has children.
$collection->allSubCollections()PageArrayAll descendant collections at any depth.
$collection->isRoot()boolWhether this is a top-level collection.
$collection->depth()intDepth in the hierarchy (root = 0).
$collection->breadcrumbPath()arrayAncestor chain from root to this collection.
$collection->parentCollection()Page|nullParent collection, or null if root.

Example: rendering a collection gallery

$collection = $pages->get('template=pkd-mediahub-collection, name=team-photos');

if ($collection->id) {
    echo "<h2>{$collection->title} ({$collection->assetCount()} images)</h2>";

    foreach ($collection->assets() as $asset) {
        $img = $asset->image();
        if (!$img) continue;
        echo "<img src='{$img->size(300, 300)->url}' alt='{$asset->altText()}'>";
    }
}

Example: collection tree with counts

$rootCollections = $pages->find(
    'template=pkd-mediahub-collection, parent.template=pkd-mediahub-collections, sort=sort'
);

foreach ($rootCollections as $collection) {
    $indent = str_repeat('  ', $collection->depth());
    echo "{$indent}{$collection->title} ({$collection->allAssetCount()} total)\n";

    foreach ($collection->subCollections() as $sub) {
        echo "  {$indent}{$sub->title} ({$sub->assetCount()})\n";
    }
}

Querying crops

Crop pages use the template pkd-mediahub-crop. You can query them directly, though most of the time you'll access crops through the parent asset's methods (see Crops API).

// All square crops across the library
$squareCrops = $pages->find('template=pkd-mediahub-crop, pkd_mhc_preset_key=square');

// All crops for a specific asset
$assetId = 1234;
$crops = $pages->find("template=pkd-mediahub-crop, pkd_mhc_master_id=$assetId");

Field names reference

When building selectors, use these field names:

FieldTypeOn template
pkd_mediahub_imageFieldtypeImageAsset
pkd_mediahub_fileFieldtypeFileAsset
pkd_mediahub_altTextAsset
pkd_mediahub_aboutTextAsset
pkd_mediahub_mimeTextAsset
pkd_mediahub_tagsPage referenceAsset
pkd_mediahub_collectionsPage referenceAsset
pkd_mhc_master_idPage referenceCrop
pkd_mhc_preset_keyTextCrop
pkd_mhc_imageFieldtypeImageCrop
pkd_mhc_crop_dataTextarea (JSON)Crop