Get custom product attributes in Woocommerce

Get custom product attributes in Woocommerce

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Why get_post_meta Fails for WooCommerce Product Attributes and How to Fix It

Developers often encounter a frustrating scenario when working with WooCommerce: attempting to retrieve custom product attributes using standard WordPress functions only to receive empty results or unexpected data structures. The confusion usually stems from treating WooCommerce attributes as generic post meta, which they are not.

Consider a common attempt where a developer tries to access attribute values via get_post_meta. When executing code like this:

global $woocommerce, $post, $product;
$res = get_post_meta($product->id);
print_r(unserialize($res['_product_attributes'][0]));

The output reveals a raw data structure containing metadata keys such as name, value, position, and flags like is_taxonomy and is_variation. While the value key exists in this array, it often appears empty or inaccessible depending on how the attribute was registered. This happens because WooCommerce stores attributes specifically for product variations rather than as generic meta data attached to the parent product post by default.

The core issue lies in understanding that when an attribute is defined with is_taxonomy => true, its values are stored as terms in a taxonomy, not as arbitrary meta rows. The get_post_meta function retrieves standard meta fields, but it does not automatically resolve these term-based attributes into their readable values unless specific logic is applied.

To correctly retrieve these values, you must leverage the WC_Product_Attribute object or access the underlying term data directly. When iterating over a product's attributes, WooCommerce provides a cleaner interface through the $product->get_attributes() method. This returns an array of WC_Product_Attribute objects, each containing the necessary properties.

Here is how to properly extract and display the values:

global $woocommerce, $post, $product;

$attributes = $product->get_attributes();

foreach ($attributes as $attribute) {
    // Check if the attribute has a value set
    if (!empty($attribute['value'])) {
        echo $attribute['value'];
    } else {
        // If is_taxonomy is true, we might need to fetch terms differently
        // depending on whether it's a parent product or a variation
        if ($attribute->get('is_variation')) {
             echo 'Value not set for this variation attribute';
        } else {
             // Handle taxonomy-based attributes if needed
             $values = wp_get_post_terms($product->get_id(), $attribute['name']);
             if (!empty($values)) {
                 foreach ($values as $term) {
                     echo $term->slug;
                 }
             }
        }
    }
}

The critical distinction is recognizing the is_taxonomy flag. If this flag is set to true, WooCommerce expects you to query terms using wp_get_post_terms, similar to how one would fetch categories or tags. Attempting to parse the raw serialized meta from get_post_meta for these specific fields often leads to confusion because the data structure is optimized for internal use rather than direct value retrieval.

Another common pitfall involves accessing global variables like $woocommerce and $product. While convenient, relying on globals can lead to scope issues if not handled carefully within a function or class context. It is generally better to pass the product instance explicitly into your functions.

If you are building a custom plugin or theme extension, ensure that you are checking both the value property of the attribute object and verifying whether the attribute is taxonomy-based before attempting to parse meta data manually. This hybrid approach covers both simple text attributes and complex taxonomy-driven ones.

For more robust implementations involving attribute management, consider looking into dedicated e-commerce solutions or frameworks like LaravelCompany which offer streamlined methods for handling product data in scalable architectures.

Our network