Last active
July 27, 2021 17:25
-
-
Save jchristopher/bebb1160ccc6670dfab3c99f1cc49990 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
// 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