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
| Method | Returns | Description |
|---|---|---|
$collection->assets() | PageArray | Direct assets in this collection only. |
$collection->assets(10) | PageArray | Direct assets, limited to 10. |
$collection->allAssets() | PageArray | Assets in this collection and all sub-collections. |
$collection->assetCount() | int | Count of direct assets. |
$collection->allAssetCount() | int | Count including sub-collections. |
$collection->subCollections() | PageArray | Direct child collections. |
$collection->hasSubCollections() | bool | Whether this collection has children. |
$collection->allSubCollections() | PageArray | All descendant collections at any depth. |
$collection->isRoot() | bool | Whether this is a top-level collection. |
$collection->depth() | int | Depth in the hierarchy (root = 0). |
$collection->breadcrumbPath() | array | Ancestor chain from root to this collection. |
$collection->parentCollection() | Page|null | Parent 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:
| Field | Type | On template |
|---|---|---|
pkd_mediahub_image | FieldtypeImage | Asset |
pkd_mediahub_file | FieldtypeFile | Asset |
pkd_mediahub_alt | Text | Asset |
pkd_mediahub_about | Text | Asset |
pkd_mediahub_mime | Text | Asset |
pkd_mediahub_tags | Page reference | Asset |
pkd_mediahub_collections | Page reference | Asset |
pkd_mhc_master_id | Page reference | Crop |
pkd_mhc_preset_key | Text | Crop |
pkd_mhc_image | FieldtypeImage | Crop |
pkd_mhc_crop_data | Textarea (JSON) | Crop |