Как скрыть товары (по типу товара) из архивов WooCommerce?
Сайт на WordPress с плагином WooCommerce. Хочу сделать доступ к определенному типу товаров только по ссылке. На ум пришло лишь скрыть их на всех страницах архивах. Нашла в гугле функцию, которая скрывает по таксономии, но не могу переделать так, что бы скрывало по типу продукта - вариативный.
function custom_pre_get_posts_query( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'clothing' ), // Don't display products in the clothing category on the shop page.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
Ответы (1 шт):
Автор решения: WP Punk
→ Ссылка
Вариативный продукт имеет пост тип product_variation.
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_admin() && $q->is_main_query() ) {
$post_types = $q->get( 'post_type' );
$post_types = is_array( $post_types ) ? $post_types : [ $post_types ];
$post_types = array_diff( $post_types, [ 'product_variation' ] );
$q->set( $post_types );
}
}
add_action( 'woocommerce_product_query', 'custom_pre_get_posts_query' );
В условие if ( ! $q->is_admin() && $q->is_main_query() ) { добавьте еще что-то чтобы исключать и не исключать вариативные товары)