Help topics

Add facet filters with counts to a list

Facets give a filter sidebar the data it needs. They show a count for each value, such as how many products are red, and the lowest and highest price of the matching entries. Morphic keeps the counts up to date when an entry is saved.

Before you start

  • Morphic Pro is required.
  • You need the WordPress Administrator role.
  • The fields you filter by must be one of the kinds listed in step 4.

Turn on facets for a model

  1. Go to Morphic > Models and open the model.
  2. Select the Facets tab.
  3. Select Turn on facets.
  4. In Filter by, select the fields. Options, text up to 256 characters, numbers, yes/no, date and time, media, WordPress content, lists and relations are offered.
  5. In Ranges, select number or date fields, such as price.
  6. In Keep counts per, select the fields to count by, usually the category. Choose the facets first.
  7. In Only include when, add the conditions that entries must meet. If the model has drafts, only published entries are counted.
  8. Select Save changes.

To turn facets off, select Turn off and then Turn off in the Turn off facets? dialog. Morphic removes the stored facet rows in the background.

Read the answer

Call the facets route for the model, facets/<model>, under your site's REST address. Filter with filter[<field>]. Several values of one field mean any of them. Different fields must all match.

curl -g "<REST address>facets/product?filter[colour]=red,blue&filter[size]=M&min[price]=10&max[price]=50&sort=price&limit=24&offset=0"

The answer has these keys:

  • total: all matching entries.
  • items: one page of entries. The page is sorted by created, updated or a range field. Add - before the name for descending order. The default is -created.
  • facets: each value with its count. Relations also have a label.
  • ranges: the min and max of each range field.
  • stats and groups: only when the request has stats or group. See Sums, averages and groups below.

Pages that only list entries can skip the counting. Add counts=0, ranges=0 and total=0. In PHP, pass 'counts' => false, 'ranges' => false and 'total' => false.

Sums, averages and groups

Add stats with one or more range fields to get count, sum, min, max and avg of the matching entries. Add group with a facet field to get the same numbers for each value of that field, for example the average price for each brand.

curl -g "<REST address>facets/product?filter[category]=shoes&stats=price&group=brand&counts=0&limit=1"

In PHP, call morphic_facets() with the type, the filters and the arguments.

Change how remembered answers are kept

Go to Morphic > Settings and select Facets. Each setting shows its default.

  • Remember results turns storing answers on or off. The default is on.
  • Only remember answers that took at least sets the milliseconds an answer must take to be stored. The default is 10.
  • Keep remembered results for sets how many hours an unused answer is kept. The default is 24.

Stored answers stop being used when an entry of the model is saved, so nobody sees an outdated list.

Export for analysis

Administrators can download a model as Parquet. Open the model's list, select More, then Export as Parquet. To send a Parquet or CSV file on a schedule, see Email a scheduled export of a model.