How to exclude specific product in WooCommerce shop page

You need to add this code to your child theme’s functions.php file or via a plugin that allows custom functions to be added, such as the Code snippets plugin. Please don’t add custom code directly to your parent theme’s functions.php file as this will be wiped entirely when you update the theme.

/**
 * WooCommerce - Exclude Products from Shop Page using IDs.
 */
add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );

function custom_pre_get_posts_query( $q ) {
  if ( ! $q->is_main_query() ) return;
  if ( ! $q->is_post_type_archive() ) return;
  
  if ( ! is_admin() && is_shop() ) {
    // Replace 376 and 388 with your products IDs. Separate each ID with a comma.
    $q->set( 'post__not_in', array(376, 388) );
  }
  remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
0 Points