Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active September 30, 2020 16:44
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/dcdb0a50047041b121cb8689d6d1f6e0 to your computer and use it in GitHub Desktop.
Save jchristopher/dcdb0a50047041b121cb8689d6d1f6e0 to your computer and use it in GitHub Desktop.
Include WooCommerce Product Drafts in SearchWP Admin Searches
<?php
// Add Drafts to Products when indexing and searching in Admin.
add_filter( 'searchwp\post_stati\product', function( $stati ) {
$stati[] = 'draft';
$stati[] = 'private';
return $stati;
}, 30 );
// Remove Drafts from front end searches.
add_action( 'searchwp\query\before', function() {
if ( ! ( is_admin() && ! wp_doing_ajax() ) ) {
add_filter( 'searchwp\query\mods', function( $mods ) {
$mod = new \SearchWP\Mod(
\SearchWP\Utils::get_post_type_source_name( 'product' )
);
$mod->set_where( [ [
'column' => 'post_status',
'value' => [ 'draft', 'private' ],
'compare' => 'NOT IN',
] ] );
$mods[] = $mod;
return $mods;
} );
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment