Last active
August 29, 2015 14:05
-
-
Save jchristopher/6d2d45c9c4254ee7bdfc to your computer and use it in GitHub Desktop.
Exclude a bbPress Forum, all of it's Topics, and all Replies for those Topics from SearchWP results
This file contains 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 | |
// exclude anything to do with Forum ID 133643 from search results in SearchWP | |
function my_exclude_private_bbpress_forum_and_children( $ids, $engine, $terms ) { | |
$forum_id = 133643; | |
// exclude Topics from that Forum | |
$topic_args = array( | |
'post_type' => 'topic', | |
'posts_per_page' => -1, | |
'fields' => 'ids', | |
'post_parent' => $forum_id, | |
'suppress_filters' => true, | |
); | |
$topic_ids = new WP_Query( $topic_args ); | |
$topic_ids = $topic_ids->posts; | |
// if there are no posts we get array( 0 => '0' ) back | |
if ( 1 == count( $topic_ids ) && $topic_ids[0] == 0 ) { | |
$topic_ids = array(); | |
} | |
// exclude Replies to those Topics | |
$reply_args = array( | |
'post_type' => 'reply', | |
'posts_per_page' => -1, | |
'fields' => 'ids', | |
'post_parent__in' => $topic_ids, | |
'suppress_filters' => true, | |
); | |
$reply_ids = new WP_Query( $reply_args ); | |
$reply_ids = $reply_ids->posts; | |
// if there are no posts we get array( 0 => '0' ) back | |
if ( 1 == count( $reply_ids ) && $reply_ids[0] == 0 ) { | |
$reply_ids = array(); | |
} | |
$ids = array_merge( $ids, array( $forum_id ), $topic_ids, $reply_ids ); | |
return $ids; | |
} | |
add_filter( 'searchwp_exclude', 'my_exclude_private_bbpress_forum_and_children', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment