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' );
}