Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active July 27, 2021 17:25
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/bebb1160ccc6670dfab3c99f1cc49990 to your computer and use it in GitHub Desktop.
Save jchristopher/bebb1160ccc6670dfab3c99f1cc49990 to your computer and use it in GitHub Desktop.
<?php
// Step 1: tell SearchWP to index Drafts, Pending, and Private entries in addition to its default post stati.
add_filter( 'searchwp\post_stati', function( $post_stati, $args ) {
$post_stati[] = 'draft';
$post_stati[] = 'pending';
$post_stati[] = 'private';
return $post_stati;
}, 20, 2 );
// Step 2: limit post stati during searches, per post type. By default
// SearchWP is going to respect the stati we defined in Step 1!
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
// If this is the 'admin' Engine, search all post stati.
if ( 'admin' === $query->get_engine()->get_name() ) {
return $mods;
}
// Only return WP_Posts with 'publish' post status.
foreach ( $query->get_engine()->get_sources() as $source ) {
if ( 'post' . SEARCHWP_SEPARATOR !== $source->get_name() ) {
continue;
}
$mod = new \SearchWP\Mod( $source );
$mod->set_where( [ [
'column' => 'post_status',
'value' => [ 'publish' ],
'compare' => 'IN',
] ] );
$mods[] = $mod;
}
return $mods;
}, 20, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment