Last active
February 8, 2022 10:29
-
-
Save MarcJandt/86541e178becab401577c288f638ac44 to your computer and use it in GitHub Desktop.
FacetWP hide posts if unfiltered
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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