built-in facet type
Custom Facet Types
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:
- The facet class
- 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 |
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 |
settings_html() | (optional) Outputs admin settings HTML |
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:
add_filter( 'facetwp_facet_types', function( $types ) {
include( dirname( __FILE__ ) . '/class-facet.php' );
$types['future_past'] = new FacetWP_Facet_Future_Past();
return $types;
});