facetwp_facet_filter_posts
Overview
This hook lets you override the filtering logic for (a) specific facet(s).
By default, FacetWP uses its own index table to determine matching posts. With this filter, you could hook into custom database tables (e.g. for doing real-time booking availability) or 3rd party services.
This hook can be used to pass a custom array of matching post IDs for a specific facet, or with specific facet choices selected. But it 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 meta ], 'selected_values' => [] ];
Returns
An array of post IDs, or FALSE
to use FacetWP’s default filtering logic.
Usage example
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 );