-
-
Save searchwpgists/70c302eddeffaff26cfecb445cd9f3b1 to your computer and use it in GitHub Desktop.
Include drafts in SearchWP Supplemental Engine
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 | |
/** | |
* Include Drafts in SearchWP Supplemental Engine results. | |
* | |
* NOTE: In order for this to work we need to first tell SearchWP to index | |
* ALL of the potential post stati. We can then curate which post stati | |
* to consider during searches; it is a two-step process. | |
*/ | |
// Step 1: tell SearchWP to index Drafts in addition to its default post stati. | |
add_filter( 'searchwp\post_stati', function( $post_stati, $args ) { | |
$post_stati[] = 'draft'; | |
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 'supplemental' Engine, search all post stati. | |
if ( 'supplemental' === $query->get_engine()->get_name() ) { | |
return $mods; | |
} | |
// Only return WP_Posts with 'publish' post status. | |
foreach ( $query->get_engine()->get_sources() as $source ) { | |
$flag = 'post' . SEARCHWP_SEPARATOR; | |
if ( 'post.' !== substr( $source->get_name(), 0, strlen( $flag ) ) ) { | |
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