Integrate WP Hide Post and SearchWP

Last updated December 18, 2018 « Knowledge Base

WP Hide Post is a plugin that allows you to (among other things) exclude posts from search results.

Screen Shot 2014-09-02 at 8.25.38 AM

You will need to tell SearchWP to utilize the data stored by this plugin and in turn exclude results as well. You can achieve this integration by adding the following to your theme’s functions.php:

<?php
/**
* Integrate WP Hide Post and SearchWP
* @link https://searchwp.com/docs/kb/integrate-wp-hide-post-and-searchwp/
*
* @param $ids
* @param $engine
* @param $terms
*
* @return array Excluded post IDs
*/
function wp_hide_posts_searchwp_exclude( $ids, $engine, $terms ) {
$args = array(
'post_type' => 'any',
'post_status' => 'any',
'nopaging' => true,
'fields' => 'ids',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_wplp_post_search',
'value' => true,
),
array(
'key' => '_wplp_page_search',
'value' => true,
),
),
);
$wp_hide_posts_ids = get_posts( $args );
if ( ! empty( $wp_hide_posts_ids ) ) {
$ids = array_merge( $ids, $wp_hide_posts_ids );
$ids = array_map( 'absint', $ids );
$ids = array_unique( $ids );
}
return $ids;
}
add_filter( 'searchwp_exclude', 'wp_hide_posts_searchwp_exclude', 10, 3 );
view raw gistfile1.php hosted with ❤ by GitHub

WP Hide Post hides posts from search results by flagging them with a Custom Field called _wplp_post_search so we need to tell SearchWP to exclude any posts with that meta_key. We’ll do that by using SearchWP’s searchwp_exclude hook and WordPress’ get_posts function to retrieve the IDs of all the posts that have been excluded from search by WP Hide Post. We’ll then send those IDs back to SearchWP and as a result they will be excluded by SearchWP.

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