// facetwp_facets

Overview

Dynamically register new facets.

Parameters

  • $facets | array | An array of existing facets

Usage

The following example will add a new search facet.

add_filter( 'facetwp_facets', function( $facets ) {
    $facets[] = array(
        'label' => 'My Search',
        'name' => 'my_search',
        'type' => 'search',
        'search_engine' => '',
        'placeholder' => 'Enter keywords',
    );
    return $facets;
});

We’re simply adding a new element to the $facets array. Every facet has a label, name, and type property (and most also have source).

Available settings

To see a facet type’s available settings, create a new facet and save it. Then, go into the Settings tab and export it. Finally, paste the export code into https://jsonbrowse.com/ to show the settings visually.

How it uses the admin UI

When a facet is registered programmatically, it’ll appear in the admin UI.

programmatic-facet-admin-ui

A notice will appear for code-based facets. To modify the facet, click the Unlock button.

When unlocked, FacetWP will save the facet’s settings in the database. If a facet exists in both code and the database, the database version will take precedence.

To switch back to the code-based settings, simply delete the facet from the admin UI.  On next refresh, the facet will re-appear in the admin UI (along with the notice).