You can create your own facet types with FacetWP.

Here’s an example plugin that lets users filter by Future or Past date:

View the code

https://github.com/FacetWP/facet-future-past

Anatomy of a facet type

Each facet type consists of 2 main parts:

  1. The facet class
  2. The facet registration (using the facetwp_facet_types hook)

The facet class

Each facet type inherits from the FacetWP_Facet class and contains most (if not all) of these methods:

Method Purpose
__construct() Set the facet type’s label and (optional) settings fields
load_values( $params ) (optional) Pulls facet choices from the database. Not all facet types (e.g. number range) use this method. The returned data is passed into render() via $params['values']
render( $params ) Generates the HTML output
filter_posts( $params ) Returns an array of post IDs that match the selected values for this facet
admin_scripts() Outputs custom scripts or CSS for the admin settings page
front_scripts() Outputs custom scripts or CSS for front-facing facet pages
register_fields()
(optional) Add custom settings fields

The above (GitHub) example uses all of the above methods, except for admin_scripts.

Registering the facet type

Here’s how we’re registering the future_past facet type:

How to use custom PHP code?

PHP code can be added to your (child) theme's functions.php file. Alternatively, you can use the Custom Hooks add-on, or a code snippets plugin. More info

add_filter( 'facetwp_facet_types', function( $types ) { include( dirname( __FILE__ ) . '/facet-future-past/class-facet.php' ); $types['future_past'] = new FacetWP_Facet_Future_Past(); return $types; });

See also

Last updated: May 24, 2024