Documentation

View the installation guide, browse the Knowledge Base, find out about SearchWP's many hooks, get support

Available since: 1.6

searchwp_exclude

View Parameters »

You can exclude specific post IDs on the SearchWP settings screen, but if you’d like to exclude post IDs programmatically you can do that with this filter.

Example: If you want to exclude a single a single post, add the following to your active theme’s functions.php:

<?php
function my_searchwp_exclude( $ids, $engine, $terms )
{
$ids[] = 78; // forcefully exclude post 78
return $ids;
}
add_filter( 'searchwp_exclude', 'my_searchwp_exclude', 10, 3 );
view raw gistfile1.php hosted with ❤ by GitHub

You can also retrieve post IDs dynamically and return those programmatically.

Example: If you want to exclude posts flagged with a taxonomy term, add the following to your active theme’s functions.php:

<?php
function my_searchwp_exclude( $ids, $engine, $terms ) {
$entries_to_exclude = get_posts(
array(
'post_type' => 'any',
'nopaging' => true,
'fields' => 'ids',
'tax_query' => array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => array( 'category-1' ),
),
),
)
);
$ids = array_unique( array_merge( $ids, array_map( 'absint', $entries_to_exclude ) ) );
return $ids;
}
add_filter( 'searchwp_exclude', 'my_searchwp_exclude', 10, 3 );
view raw functions.php hosted with ❤ by GitHub

Example: If you want to exclude an entire bbPress Forum, it’s Topics, and all Replies to those Topics add the following to your active theme’s functions.php:

<?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 );
view raw gistfile1.php hosted with ❤ by GitHub

Parameters

Parameter Type Description
$ids Array

Any post IDs that are considered excluded already

$engine String

The engine currently in use

$terms Array

Submitted search terms

Fix Search on Your Site. No Coding Required!

Now you can utilize all of the content that's gone unrecognized by native WordPress search instantly with SearchWP

Get SearchWP