fixing undefined var error within wpdb query and check if term is wp_error before trying to access its properties