The code below with the 19 is working correctly as it is copied from the woocommerce website. Unfortunate I can't get it working to replace the 19 with $producten so that $producten contains an array with all categories.

$producten contains multiple categories that need to be hidden and are defined in the database. 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归因
不隶属于 StackOverflow
scroll top