Overview

This hook lets you override the filtering logic for (a) specific facet(s). It can be used to pass a custom array of matching post IDs for a specific facet, or with specific facet choices selected.

By default, FacetWP uses its own index table to determine matching posts. You could use this filter to hook into custom database tables (e.g. for doing real-time booking availability) or 3rd party services, as shown in the first example below.

Or you could let a specific facet use a different filtering logic on specific pages or templates, as shown in the second example.

Note that this hook has no knowledge of the original matching posts for the facet or facet choice(s) (without using this hook). If you want to customize the original matching post IDs for a facet (choice), for example to remove one or more posts, use the facetwp_filtered_post_ids hook instead. And to customize the post IDs also before any facet filtering has happened, use facetwp_pre_filtered_post_ids.

Parameters

  • $return | mixed | FALSE (default), or an array of post IDs
  • $params | An associative array of parameters (see below)

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

$params = [ 'facet' => [ 'label' => 'My facet', 'name' => 'my_facet', 'type' => 'checkboxes', 'source' => 'cf/the_field_name', // ... other facet settings (different for each facet type) ], 'selected_values' => [] ];

Returns

An array of post IDs, or FALSE to use FacetWP’s default filtering logic.

Examples

Filter available rooms for selected dates

In this example, we fetch available rooms from a custom database table for selected dates in a Date Range facet:

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_filter_posts', function( $return, $params ) { $selected_values = $params['selected_values']; if ( 'availability' == $params['facet']['name'] ) { $start_date = $selected_values[0]; $end_date = $selected_values[1]; // Get post IDs of available rooms between the facet's selected dates, from another database table, with a custom function $post_ids = get_available_rooms( $start_date, $end_date ); return $post_ids; } return $return; }, 10, 2 );

Change facet logic

The following example shows how to change the facet logic (“AND (match all)” or “OR (match any)”) for a specific facet. Of course this only works for facet types that have a “Facet logic” setting, like the Checkboxes facet.

You could use this to change the facet’s filtering logic on specific pages or templates. In this case, we change it to “OR (match any)” if the facet is on a specific category archive.

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_filter_posts', function( $return, $params ) { if ( is_category( 'my-category' ) ) { if ( 'my_facet_name' == $params['facet']['name'] ) { // Replace 'my_facet_name' with the name of your facet $params['facet']['operator'] = 'or'; // Set to "OR (match any)" $matches = FWP()->facet->facet_types[ $params['facet']['type'] ]->filter_posts( $params ); return $matches; } } return $return; }, 10, 2 );

More examples

See also

Last updated: March 14, 2025