Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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