Last active
March 23, 2021 14:54
-
-
Save jchristopher/57cd2cf937bf71fc6cf8 to your computer and use it in GitHub Desktop.
Add support for Groups to SearchWP
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 | |
// 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 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 !