Menu
All plugins

Nested Query for Bricks

A Bricks element that gives each group in a loop its own Load more button. Put it inside a Terms query loop and every term lists its own posts or products, and loads the next page of them without touching any other term’s list. Each list can also have a card layout, page numbers instead of a button, its own search box and a different order. A separate filter element can drive every list at once.

Version: 0.4.0 · Requires: WordPress 6.0+, PHP 7.4+, Bricks · See the live demo

What it does

  • Adds a Nested Posts element to Bricks.
  • Lists the posts (or products) assigned to the current term of the loop it sits in.
  • Shows a Load more button only for terms with more posts than fit on the first page.
  • Loads the next page by AJAX and adds it to that term’s list only. Other terms keep their own place.
  • Can show page numbers with previous and next buttons instead of Load more. Each list has its own pager.
  • Shows the items as a plain list or as cards, with the featured image, date, excerpt and a WooCommerce price if you want them.
  • Orders each list by date, modified date, title or menu order, in either direction.
  • Gives each list an optional search box. It searches that term’s posts only, and Load more carries on from the results.
  • Works with any public taxonomy, including a loop that mixes several. The requests are not tied to one page, so it also suits templates and cached pages.
  • Puts a list back to where it was if a parent loop replaces it by AJAX.
  • Adds a Nested Posts Filter element: one search box, tag dropdown or order dropdown that controls every list in a group at once.

Installation

  1. Upload and activate the plugin. Bricks must be active.
  2. In the Bricks editor search for Nested Posts (General group).

Quick start

  1. Add a Container or Block and switch on Query loop. Choose Terms as the type, and pick a taxonomy, such as Categories.
  2. Inside it add a heading with .
  3. Below the heading add Nested Posts.
  4. Set the Post type slug. For normal posts that is post. For WooCommerce it is product with a loop over product_cat. The taxonomy is taken from the loop by itself.
  5. Set the number of items per page and choose a layout.
  6. Open the front end. The builder shows a short note if the element is not inside a Terms loop.

If the Terms loop shows terms from other taxonomies as well, set the taxonomy on the loop itself. Some versions of Bricks need the taxonomy chosen there before they limit the loop to it.

Settings

  • Post type slug — the post type to list (default: post)
  • Taxonomy slug (optional) — empty uses the taxonomy of each term in the loop. Fill it in to show the lists for that taxonomy only and skip terms of any other taxonomy in a mixed loop
  • Include posts of child terms — also list posts that sit in the term’s children
  • Items per page — 1 to 24 (default: 4)
  • Order by and Order — date published (default), date modified, title or menu order, ascending or descending
  • Layout — List (default) or Cards, with Cards per row from 1 to 4. Cards drop to one column on phones
  • Show featured image, Show date, Excerpt words and Show WooCommerce price
  • Search box above each list and its placeholder
  • Pagination — Load more button (default) or Page numbers
  • Filter group (optional) — joins this list to the Nested Posts Filter elements with the same group name
  • Button label — the text on the button (default: Load more)

How it works

  • Each copy of the element in the loop is identified by the element and the term it belongs to, for example abc123-term-29.
  • The page sends the term, the page number and a signature of the settings to admin-ajax.php. The server rebuilds the query itself, so the browser can never ask for different posts, taxonomies, orders or page sizes than the ones the element was set up with. The only thing a visitor can add is a search word, which is cleaned and cut to 100 characters.
  • The signature is made from the element, the term and the settings. There is no nonce and the request is not tied to one page, so cached pages and templates keep working. Only public post types and taxonomies, and published posts without a password, can be listed.
  • One click handler covers the whole page, and the script remembers how far each list was opened. If a parent loop replaces the lists, each one reopens to the same page and search word.

Nested Posts Filter

Place this element anywhere on the page, outside the loop, and give it the same Group name as the Nested Posts elements it should control. Add as many as you like.

  • Search box — searches every list in the group
  • Taxonomy dropdown — for example tags. Choosing one shows only the posts with that term in every list. It needs a taxonomy other than the one the loop is over
  • Order dropdown — newest, oldest, title A to Z or Z to A

The filters combine. Each list goes back to its first page when a filter changes, shows how many it found, and keeps the filter when you load more or turn the page. A list with no match shows the no-match message.

Styling

Each term’s list is a .bnq section holding an optional .bnq-search form, a .bnq-items list of .bnq-item rows (with .bnq-thumb, .bnq-title, .bnq-date, .bnq-price and .bnq-excerpt inside), a .bnq-load button or a .bnq-pager with .bnq-page buttons, and a .bnq-status line. The filter element is a .bnq-filter holding a .bnq-filter-input or .bnq-filter-select. Style them with custom CSS. The default look is plain and square.

Not included

  • It does not connect to the native Bricks filter and pagination elements. Those work on one Bricks query, and this element runs its own query for each term. The Nested Posts Filter and the built-in pager do the same jobs for these lists.
  • Results are in the order you pick. There is no random order, because it would repeat posts across pages.
  • The loop has to be a Terms loop. A loop over posts or users does not give the element a term to list.

Changelog

  • 0.4.0 — Nested Posts Filter element: one search box, taxonomy dropdown or order dropdown for every list in a group.
  • 0.3.0 — Page numbers with previous and next buttons as an alternative to Load more, with a pager for each list.
  • 0.2.0 — Cards layout with image, date, excerpt and WooCommerce price. Order controls. Search box for each list. Child terms option. No longer tied to one page, so it can sit in templates and cached pages. Each list reopens where it was after a parent refresh.
  • 0.1.1 — The taxonomy follows the loop by default and other taxonomies are skipped. No more nonce, so cached pages keep working. The button is hidden when a request goes past the last page. Square, quieter default styling. Focus moves to the first new item after loading.
  • 0.1.0 — First version.