Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active March 23, 2021 14:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/57cd2cf937bf71fc6cf8 to your computer and use it in GitHub Desktop.
Save jchristopher/57cd2cf937bf71fc6cf8 to your computer and use it in GitHub Desktop.
Add support for Groups to SearchWP
<?php
// SearchWP 4.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb, $wp_query;
if ( class_exists( 'Groups_Post_Access' ) ) {
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->posts );
$mod->on( 'ID', [ 'column' => 'id' ] );
$mod->raw_where_sql( function( $runtime ) use ( $wpdb, $wp_query ) {
return ' 1=1 ' . str_replace(
$wpdb->posts . '.ID',
"{$runtime->get_foreign_alias()}.id",
Groups_Post_Access::posts_where( '', $wp_query )
);
} );
$mods[] = $mod;
}
return $mods;
}, 20, 2 );
// SearchWP 3.
function my_searchwp_where( $where, $engine ) {
global $wp_query;
if ( class_exists( 'Groups_Post_Access' ) ) {
$where .= ' ' . Groups_Post_Access::posts_where( '', $wp_query );
}
return $where;
}
add_filter( 'searchwp_where', 'my_searchwp_where', 10, 2 );
@anaisponte
Copy link

Thanks for this !
I got an error when trying to use it : Notice: Undefined property: wpdb::$query

Changed the code to :

function my_searchwp_where( $where, $engine ) { global $wp_query; if ( class_exists( 'Groups_Post_Access' ) ) { $where .= ' ' . Groups_Post_Access::posts_where( '', $wp_query ); } return $where; } add_filter( 'searchwp_where', 'my_searchwp_where', 10, 2 );

and now everything works fine !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment