Skip to content

Instantly share code, notes, and snippets.

@MikeDacre
Created April 26, 2016 05:21
Show Gist options
  • Save MikeDacre/4e1bc9526e02a3112a0e1cb620ceabb9 to your computer and use it in GitHub Desktop.
Save MikeDacre/4e1bc9526e02a3112a0e1cb620ceabb9 to your computer and use it in GitHub Desktop.
Make SearchWP work with Members

To use this code, pop index.php and searchwp-members.php into a directory named searchwp-members, zip it up with zip -r searchwp-members.zip searchwp-members, and install the zip file in wordpress.

It works with ajax live search also.

<?php
// Silence is golden...
<?php
/*
Plugin Name: SearchWP Members Integration
Plugin URI: https://searchwp.com/
Description: Integrate SearchWP and PrivateContent
Version: 1.0
Author: Mike Dacre
Author: Jonathan Christopher
Author URI: https://searchwp.com/
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
// exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! defined( 'SEARCHWP_MEMBERS_VERSION' ) ) {
define( 'SEARCHWP_MEMBERS', '1.0' );
}
class SearchWP_Members {
function __construct() {
add_action( 'searchwp_indexer_pre', array( $this, 'indexer_pre' ) );
add_filter( 'searchwp_include', array( $this, 'wp_members_searchwp_exclude' ), 10, 3 );
}
function indexer_pre() {
remove_filter( 'pre_get_posts', 'pg_query_filter', 999 );
}
function wp_members_searchwp_exclude( $ids, $engine, $terms ) {
$args = array(
'post_type' => 'any',
'post_status' => 'any',
'nopaging' => true,
'fields' => 'ids',
);
$all_post_ids = get_posts( $args );
$wp_members_exclude_ids = array();
foreach ( $all_post_ids as $post_id ):
if ( ! members_can_current_user_view_post( $post_id ) ) :
$wp_members_exclude_ids[] = $post_id;
endif;
endforeach;
if ( ! empty( $wp_members_exclude_ids ) ) {
$ids = array_merge( $ids, $wp_members_exclude_ids );
$ids = array_map( 'absint', $ids );
$ids = array_unique( $ids );
}
return $ids;
}
}
new SearchWP_Members();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment