→ 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.

function mytheme_register_facets( $facets ) {
    $facets[] = array(
        'label' => 'My Search',
        'name' => 'my_search',
        'type' => 'search',
        'search_engine' => '',
        'placeholder' => 'Enter keywords',
    );
    return $facets;
}
add_filter( 'facetwp_facets', 'mytheme_register_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

Depending on the facet type, facets also have additional settings. See the list below for every facet type’s available settings.

{
  "label":"Test checkboxes",
  "name":"test_checkboxes",
  "type":"checkboxes",
  "source":"post_type",
  "parent_term":"",
  "orderby":"count",
  "operator":"and",
  "hierarchical":"no",
  "ghosts":"no",
  "preserve_ghosts":"no",
  "count":"10"
},
{
  "label":"Test dropdown",
  "name":"test_dropdown",
  "type":"dropdown",
  "source":"post_type",
  "label_any":"Any",
  "parent_term":"",
  "orderby":"count",
  "hierarchical":"no",
  "count":"10"
},
{
  "label":"Test fSelect",
  "name":"test_fselect",
  "type":"fselect",
  "source":"post_type",
  "label_any":"Any",
  "parent_term":"",
  "orderby":"count",
  "multiple":"no",
  "count":"10"
},
{
  "label":"Test hierarchy",
  "name":"test_hierarchy",
  "type":"hierarchy",
  "source":"post_type",
  "orderby":"count",
  "count":"10"
},
{
  "label":"Test search",
  "name":"test_search",
  "type":"search",
  "search_engine":"",
  "placeholder":""
},
{
  "label":"Test autocomplete",
  "name":"test_autocomplete",
  "type":"autocomplete",
  "source":"post_type"
},
{
  "label":"Test slider",
  "name":"test_slider",
  "type":"slider",
  "source":"post_type",
  "source_other":"",
  "compare_type":"",
  "prefix":"",
  "suffix":"",
  "format":"0,0",
  "step":"1"
},
{
  "label":"Test date range",
  "name":"test_date_range",
  "type":"date_range",
  "source":"post_type",
  "source_other":"",
  "compare_type":"",
  "fields":""
},
{
  "label":"Test number range",
  "name":"test_number_range",
  "type":"number_range",
  "source":"post_type",
  "source_other":"",
  "compare_type":""
},
{
  "label":"Test proximity",
  "name":"test_proximity",
  "type":"proximity",
  "source":"post_type",
  "unit":"mi"
}

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).