• robintrum

    (@robintrum)


    Hello all,

    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!

    https://wordpress.org/plugins/woocommerce/

The topic ‘Hide multiple product categories using an array’ is closed to new replies.