-
In the past I've worked on projects that used post filtering plugins like FilterEverything, JetFilters or FacetWP. None were ideal to work with so I was wondering if anyone had any advice on a plugin that's lightweight, easy to implement and straightforward to use in a Timber theme? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
I use factwp in most Projects that need this functionally. I map the shortcodes directly to the an array names facets in the context. So I can do this {{ facets.pager }} on a particular page or block. |
Beta Was this translation helpful? Give feedback.
-
in my site class i have a static function with base facets I reuse in multiple blocks. The facetwp_function is just a wrapper by facetwp self to prevent using shortcodes in your theme. public static function facet_base_filters()
{
return [
'per_page' => FWP()->facet->pager_args['per_page'],
'selections' => facetwp_display('selections'),
'search' => facetwp_display('facet', 'search_search'),
'search_resultsamount' => facetwp_display('facet', 'search_resultsamount'),
'pagination' => facetwp_display('facet', 'search_pagination'),
];
} Then in my block controller: public function block_context($context): array
{
$type = $this->get_field('post_type');
$items = Timber::get_posts([
'post_type' => $type,
'posts_per_page' => 6,
'facetwp' => true,
]);
$facets = WP_Lemon_Child_Site::facet_base_filters();
$facets['dynamic'] = [
[
'title' => __('Lectorate', 'wp-lemon-child'),
'output' => facetwp_display('facet', 'publication_lectorate'),
],
[
'title' => __('Year', 'wp-lemon-child'),
'output' => facetwp_display('facet', 'publication_year'),
],
[
'title' => __('Education type', 'wp-lemon-child'),
'output' => facetwp_display('facet', 'publication_type'),
],
[
'title' => __('Author', 'wp-lemon-child'),
'output' => facetwp_display('facet', 'publication_author'),
],
];
$args = [
'items' => $items,
'type' => $type,
'facets' => $facets,
];
return array_merge($context, $args);
} and in my twig file: <div class="items-overview__inner">
<div class="row">
<aside class="col-md-4">
<div class="filterwrap">
<div class="filter-header">
<h2 class="h3">{{ __('Filters', 'wp-lemon-child') }}</h2>
<button class="reset-filters" onclick="FWP.reset()">
<i class="fa-sharp fa-solid fa-xmark"></i>
{{ __('Reset all filters', 'wp-lemon-child') }}
</button>
</div>
<form class="filters">
<div class="filter-group">
{{ facets.search }}
{% for facet in facets.dynamic %}
<div class="facet-dynamic">
<div class="filter-title">{{ facet.title }}</div>
{{ facet.output }}
</div>
{% endfor %}
</div>
</form>
</div>
</aside>
<div class="col-md-8">
<div class="filterresults" aria-live="polite" role="region">
{{ facets.search_resultsamount }}
<span class="filtered">
{{ __('Filtered on:', 'wp-lemon-child') }}
{{ facets.selections }}
</span>
</div>
<div class="row facetwp-template facet-results"
data-per-page="{{ facets.per_page }}">
{% for post in items %}
{% include 'components/cards/crd-' ~ type ~ '.twig' %}
{% endfor %}
</div>
<div class="pagination">{{ facets.pagination }}</div>
</div>
</div>
</div> |
Beta Was this translation helpful? Give feedback.
in my site class i have a static function with base facets I reuse in multiple blocks. The facetwp_function is just a wrapper by facetwp self to prevent using shortcodes in your theme.
Then in my block controller: