Skip to content

Instantly share code, notes, and snippets.

@devendrabisht
Created November 22, 2023 05:49
Show Gist options
  • Save devendrabisht/5da458e6a13bd4fc9ada08954620cbc9 to your computer and use it in GitHub Desktop.
Save devendrabisht/5da458e6a13bd4fc9ada08954620cbc9 to your computer and use it in GitHub Desktop.
Forcefully show draft products on frontend - WooCommerce
function devxwp_show_draft_products_on_frontend( $query ) {
// echo '<pre>';
// print_r($query);
// echo '</pre>';
if( ! current_user_can( 'administrator' ) ) { return; }
if ( ! is_admin() && $query->is_main_query() ) {
// Not a query for an admin page.
// It's the main query for a front end page of your site.
if ( is_product_category() ) {
// It's the main query for a category archive.
// Let's change the query for category archives.
$query->set( 'post_status', array( 'publish', 'draft' ) );
}
}
}
add_action( 'pre_get_posts', 'devxwp_show_draft_products_on_frontend', 20, 1 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment