I currently have debug mode enabled in wp-config.php. I'm trying to loop through all the products in a certain category on my store.

$args = array(
    'post_type' => 'product',
    'product_cat' => 'scotch-whisky',
    'orderby' => 'title',
    'posts_per_page' => -1,
    'order' => 'ASC'

$loop = new WP_Query( $args );

I am then attempting to loop through the products and get a list of brand names. There might be five products with the brand name "Apple", however I am curating a list of brands so I only want "Apple" to appear once in the array.

To do that I loop through the products and check if the brand name is in my list, if it is I skip it, if not I add it to my list.


$distilleries = Array();

while ( $loop->have_posts() ) : $loop->the_post();
    $attr = wc_get_product_terms(get_the_id(), 'pa_distillery');
    if (
        in_array($attr[0], $distilleries) === false &&
        strlen($attr[0]) !== 0
    ) {

        array_push($distilleries, $attr[0]);


                <a href="<?php echo get_site_url(); ?>/product-category/scotch-whisky/?filter_distillery=<?php echo strtolower($attr[0]); ?>">
                    <?php echo $attr[0]; ?>





The problem appears on some iterations where I try to access $attr[0] in the if statement and get the following error:

Notice: Undefined offset: 0 in /home/www.something.com/123/public_html/wp-content/plugins/brand-list/list.php on line 87

Notice: Undefined offset: 0 in /home/www.something.com/123/public_html/wp-content/plugins/brand-list/list.php on line 88

I run a var dump on $attr[0] and $attr[0] is NULL. How can I stop this error from appearing or is there even anything I need to do or worry about?

Many thanks,


  • 1
    You need to check if $attr[0] exists using isset(), or that $attr isn’t empty(), before attempting to use $attr[0], even in an if condition. – Jacob Peattie Oct 3 '18 at 18:04
  • Yes this solved the notification. Amazing! Thanks for the quick help!! – Jethro Hazelhurst Oct 3 '18 at 23:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.