WooCommerce 特定カテゴリーをショップ一覧ページから非表示にする

お得意のお客様だけにセールを行いたい。そんな時に使えるテクニック。

WooCommerceのショップ一覧ページの表示をカテゴリーのみに設定した上で、以下のコードを子テーマのfunctions.phpに追記すると、コードの中で ID で指定したカテゴリーが、一覧から非表示になる。

なお、子カテゴリーの設定は出来ない点に注意する。

/* Hide Category on WooCommerce */
function get_subcategory_terms( $terms, $taxonomies, $args ) {
 
	$new_terms 	= array();
	$hide_category 	= array( 11 ); // 11はサンプル。ここに非表示にしたいカテゴリーIDを入れる。複数の場合はカンマ区切りで入力
 	
 	  // if a product category and on the shop page
	if ( in_array( 'product_cat', $taxonomies ) && !is_admin() && is_shop() ) {

	    foreach ( $terms as $key => $term ) {

		if ( ! in_array( $term->term_id, $hide_category ) ) { 
			$new_terms[] = $term;
		}
	    }
	    $terms = $new_terms;
	}
  return $terms;
}
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

このテクニックは、こちらのページが参考になった。感謝です。

追加の参考資料はこちら。