Ajax Filters
Mode
How it works
1 – Create an output template
<?php
/**
* Filter template: {slug}
*
* Receives:
* - $filters (array) Current filter values keyed by parameter
* - $query (WP_Query) The query that will be (re)run
* - $attrs (array) Block attributes
*
* Return a string of HTML.
*/
defined( 'ABSPATH' ) || exit;
// Example: build a list of taxonomy terms as checkboxes.
ob_start();
$terms = get_terms( [
'taxonomy' => 'sector',
'hide_empty' => false,
] );
?>
<form class="porter-filters" data-filters-form>
<?php foreach ( $terms as $term ) : ?>
<label>
<input type="checkbox"
name="sector[]"
value="<?php echo esc_attr( $term->slug ); ?>"
<?php checked( in_array( $term->slug, $filters['sector'] ?? [], true ) ); ?> />
<?php echo esc_html( $term->name ); ?>
</label>
<?php endforeach; ?>
<button type="submit">Apply</button>
</form>
<?php
return ob_get_clean();2 – Add the JavaScript
3 – Reference the template inside the block
4 – Enqueue the script
5 – Caching & CLS considerations
Summary
Piece
Location
Responsibility
Last updated