Skip to content

Instantly share code, notes, and snippets.

@MarcJandt
Last active February 8, 2022 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MarcJandt/86541e178becab401577c288f638ac44 to your computer and use it in GitHub Desktop.
Save MarcJandt/86541e178becab401577c288f638ac44 to your computer and use it in GitHub Desktop.
FacetWP hide posts if unfiltered
<?php
// using FacetWP to filter posts but hide certain category if no facet is selected
add_filter( 'facetwp_filtered_post_ids', function( $post_ids ) {
$facets = FWP()->facet->facets;
// get post IDs to hide by category
$hide_post_ids = get_posts(array(
'numberposts' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'hidden-category',
),
),
'fields' => 'ids',
));
// check whether the only facet isn't selected (unfiltered status)
if ( empty( $facets['only_facet']['selected_values'] ) ) {
return array_diff( $post_ids, $hide_post_ids );
}
// otherwise return filtered posts including those of the category above
return $post_ids;
}, 9 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment