facetwp_pre_filtered_post_ids
Overview
Choose the initial bucket of post IDs, after the WP_Query has been executed but before any facets have been processed.
This hook is especially useful as an alternative to using post__not_in
as a query argument (using a pre_get_posts hook for custom queries or WP archive queries, or a using a facetwp_query_args hook for Listing Builder queries). FacetWP uses post__in
, and WP queries have the limitation that you cannot use post__in
and post__not_in
simultaneously.
Parameters
- $post_ids | array | An array of post IDs
- $class | object | The
FacetWP_Renderer
class (see /includes/class-renderer.php)
Usage examples
Prevent post ID = 42
from appearing in the results:
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_pre_filtered_post_ids', function( $post_ids, $class ) { if ( false !== ( $key = array_search( 42, $post_ids ) ) ) { // Remove post ID 42 unset( $post_ids[ $key ] ); } return $post_ids; }, 10, 2 );
Prevent multiple post IDs from appearing in the results:
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_pre_filtered_post_ids', function( $post_ids, $class ) { $remove = [42,43,44]; // The post IDs to be removed $post_ids = array_diff($post_ids, $remove); return $post_ids; }, 10, 2 );
Show only a selection of post IDs in the results:
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_pre_filtered_post_ids', function( $post_ids, $class ) { $post_ids = [42,43,44]; // The post IDs to keep return $post_ids; }, 10, 2 );