概要
woocommerce_product_query_max_rand_cache_count
フィルタは、WooCommerceのランダム商品クエリのキャッシュの最大数を制御するために使用されます。このフィルタを用いることで、サイトのパフォーマンスを向上させつつ、特定の商品をより効果的に表示することが可能になります。以下のようなケースでよく利用されます。
- 商品ページでのランダム表示商品数の制限
- 商品一覧ページでの表示商品の多様性確保
- セール商品や特定のカテゴリー商品を表示する際の調整
- ショッピングサイトのランダム商品表示のロジックをカスタマイズ
- 特定の条件を満たす商品の優先表示
- SEO対策としての多様性の確保
構文
add_filter( 'woocommerce_product_query_max_rand_cache_count', 'custom_max_rand_cache_count' );
パラメータ
$max_count
(int): ランダムキャッシュの最大数
戻り値
- (int): 新しい最大数
使用可能なプラグインバージョン
- WooCommerce: 2.6.0以降
- WordPress: 4.0以降
サンプルコード
サンプルコード 1
add_filter( 'woocommerce_product_query_max_rand_cache_count', function( $max_count ) {
return 20; // キャッシュの最大数を20に設定
} );
このコードは、ランダム商品クエリのキャッシュの最大数を20に設定します。
サンプルコード 2
add_filter( 'woocommerce_product_query_max_rand_cache_count', function( $max_count ) {
return ( is_product_category( 'special' ) ) ? 50 : $max_count; // 特殊カテゴリーの時は50に設定
} );
「special」カテゴリーの商品表示時にキャッシュの最大数を50に設定するサンプルコードです。
サンプルコード 3
function custom_product_query_max_rand_cache_count( $max_count ) {
if ( is_user_logged_in() ) {
return 30; // ログインユーザーの場合は30に設定
}
return $max_count; // それ以外は元の最大数を使用
}
add_filter( 'woocommerce_product_query_max_rand_cache_count', 'custom_product_query_max_rand_cache_count' );
このコードは、ログインユーザーに対してランダム商品のキャッシュ最大数を30に設定するものです。
サンプルコード 4
add_filter( 'woocommerce_product_query_max_rand_cache_count', 'adjust_max_cache_count' );
function adjust_max_cache_count( $max_count ) {
return $max_count * 2; // 現在の最大数の2倍に設定
}
システムの現在の最大数の2倍にキャッシュの最大数を設定するサンプルです。
サンプルコード 5
add_filter( 'woocommerce_product_query_max_rand_cache_count', function( $max_count ) {
return 10; // 常に10に設定
} );
このコードでは、常にキャッシュの最大数を10に固定します。
この関数のアクションでの使用可能性
アクション | 使用例 |
---|---|
mu_plugin_loaded | |
registered_post_type | |
plugins_loaded | |
wp_roles_init | |
setup_theme | |
after_setup_theme | |
set_current_user | |
init | |
register_sidebar | |
wp_loaded | |
send_headers | |
parse_query | |
pre_get_posts | |
wp | |
template_redirect | |
get_header | |
wp_head |
この表では、woocommerce_product_query_max_rand_cache_count
フィルタがWooCommerceの各アクションで使用可能かどうかを示しています。使用例がない場合は空白として表示しています。