[英]Woocommerce hide multiple categories from shop using arrays
The code below with the 19 is working correctly as it is copied from the woocommerce website. 从woocommerce网站复制的下面带有19的代码可以正常工作。 Unfortunate I can't get it working to replace the 19 with $producten so that $producten contains an array with all categories. 不幸的是,我无法用$ producten代替19,因此$ producten包含所有类别的数组。
$producten contains multiple categories that need to be hidden and are defined in the database. $ producten包含多个需要隐藏的类别,并且已在数据库中定义。 Is it possible what I want to do and how? 我想做什么,怎么做?
$userid = get_current_user_id();
$result = mysql_query("SELECT productID FROM bestellingen WHERE userID='$userid'");
$producten = array();
while($row = mysql_fetch_array($result)){
array_push($producten, $row['productID']);
}
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() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 19 ), // de categorienummers
'operator' => 'NOT IN'
)));
}
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
Thanks in advance! 提前致谢!
It is solved using a session like: 使用以下会话可以解决此问题:
$result = mysql_query("SELECT productID FROM bestellingen WHERE userID='$userid'");
$producten = array();
while($row = mysql_fetch_array($result)){
array_push($producten, $row['productID']);
}
$_SESSION['PRODUCTEN'] = $producten;
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() ) {
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_SESSION['PRODUCTEN'], // Don't display products in the knives category on the shop page
'operator' => 'NOT IN'
)));
}
unset($_SESSION['PRODUCTEN']);
remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.