Query MyBB collections with MyCode and PHP

amtekkie · Today

Show collection entries anywhere on your MyBB forum: in posts, pages, blog posts and other entries with MyCode, or from themes and modules with PHP. Filter by category, tag, author and field values, sort, limit and choose a layout.

Three ways
  • [collection], [entry] and [field] MyCode in any parsed message
  • Snippets: reusable PHP blocks
  • ms_collection_query() from themes and modules

[collection]: a list of entries

[collection=recipes limit=6 category=desserts layout=cards]
AttributeMeaning
limitHow many entries (up to 100)
category, tagA category or tag by its address; a category includes its sub-categories
authorA member's user id
whereField conditions, for example where="servings>=4;level=Easy|Medium"
searchEntries containing these words
sort, orderdate, title, updated, menu, random or a sortable field; asc or desc
featured1 for featured entries only
parentChildren of an entry (hierarchical collections)
layoutcards, list, grid, table or titles
more, emptyA link text to the full listing; the text when nothing matches

Field conditions use =, !=, >, >=, <, <= and ~ (contains); = with values separated by | means "any of". Separate conditions with ;.

[entry] and [field]

[entry=recipes/chocolate-cake]          one entry as a card (layout=list|titles|cards)
[field=servings]                        a field of the entry being shown

[field] works inside an entry's own content, so a recipe can say Serves [field=servings] and show its own number.

Snippets: reusable PHP blocks

Under Content Manager → Snippets, administrators with the Install PHP code permission save small PHP blocks and use them with [snippet=name key=value], where the snippet allows it (PHP only, entries, or everywhere). Snippets live in the database, never in files, and every change is logged.

PHP: ms_collection_query()

$r = ms_collection_query(array(
    'collection' => 'recipes',
    'where'      => 'servings>=4;level=Easy',     // or array(array('servings', '>=', 4))
    'category'   => 'desserts',
    'tag'        => 'chocolate',
    'author'     => 5,
    'search'     => 'cake',
    'sort'       => 'servings',
    'order'      => 'asc',
    'limit'      => 6,
    'page'       => 1,
));
foreach ($r['entries'] as $e) {
    echo htmlspecialchars_uni($e['title']), ' ', $e['url'], ' ', (int) $e['fields']['servings'];
}
echo ms_collection_render($r['entries'], 'cards');

$e = ms_collection_entry('recipes', 'chocolate-cake');
echo ms_snippet('price_table', array('currency' => 'EUR'));

Permissions and safety

  • Queries list only live entries (published, or scheduled and due) from collections the visitor may read. From PHP you can pass 'check_perms' => false for server-side jobs.
  • Field keys are looked up in the collection's definitions, operators come from a fixed list and values are read with each field's rules, so nothing typed reaches the database unchecked.
  • A message renders at most five heavy tags ([collection], [entry], [snippet]), and a page at most fifteen, so a thread full of them can't slow the forum.

Templates

Lists use the collection's item templates (ms_coll_item_cards, ms_coll_item_list, ms_coll_item_grid, ms_coll_item_titles), which a theme can override per collection as ms_coll_{slug}_item_cards and so on. See collections.