woocommerce get list of attribute values

woocommerce get list of attribute values

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

Stefan Bogdanescu

Founder & Senior Architect

Retrieving WooCommerce Attribute Values for Filtering

When building a shop catalog, simply displaying product attributes isn't enough; you often need to extract those values to build dynamic filters. A common scenario involves defining a size attribute with terms like Small, Medium, and Large, alongside a color attribute with Red, Blue, or Green. The goal is to populate a dropdown list with these specific values rather than just the taxonomy slugs like pa_size or pa_color.

The standard approach for accessing taxonomies in WooCommerce relies on the global $woocommerce object. As noted in recent inquiries, using $woocommerce->get_attribute_taxonomy_names() successfully returns the slugs of defined attribute groups. However, this method stops short of providing the actual term values (the specific options within those groups). The core challenge lies in bridging the gap between knowing a taxonomy exists and retrieving its populated terms for display.

To solve this, you must query the WordPress Term API directly, leveraging the relationship WooCommerce has already established between products and these attributes. Since attribute values in WooCommerce are stored as terms within the product_attributes taxonomy (or custom taxonomies if defined via code), you can use get_terms() to fetch them.

The most direct implementation involves iterating through your known attribute slugs and calling get_terms(). Here is a functional pattern to retrieve values for a specific attribute:

global $woocommerce;

$attribute_slug = 'pa_size'; // Or 'size' depending on how it was registered

$args = array(
    'taxonomy'   => 'pa_' . $attribute_slug, 
    'orderby'    => 'name',
    'order'      => 'ASC'
);

$terms = get_terms( $args );

if ( ! is_wp_error( $terms ) && ! empty( $terms->terms ) ) {
    // Format output as key-value pairs for a dropdown
    foreach ( $terms->terms as $term ) {
        echo '<option value="' . esc_attr( $term->slug ) . '">' . esc_html( $term->name ) . '</option>';
    }
}

A critical detail here is the taxonomy slug. When attributes are added via the WordPress admin under "Product Attributes," WooCommerce automatically prefixes them with pa_ (e.g., pa_size). If you are using a custom plugin or theme function to register attributes, the prefix might differ, but the underlying mechanism remains the same: get_terms() expects the full taxonomy slug.

Another consideration is performance when dealing with large catalogs. Fetching terms for every single product in a loop will quickly degrade performance. If your intent is to build a global filter sidebar that applies across multiple products, it is more efficient to fetch these terms once and cache them, or rely on WooCommerce's existing AJAX filtering mechanisms which handle term retrieval server-side.

If you need to expose this data via the REST API for a headless setup, the GET /products endpoint includes an attributes field by default. This JSON response contains both the slug and the values (e.g., "values": ["Small", "Medium"]). You can parse this directly from the API response without hitting the database again, which is particularly useful for frontend frameworks like React or Vue.js that manage their own state.

One potential pitfall involves handling empty attributes. If an attribute is defined on a product but has no terms selected yet, get_terms() might return an empty array. Always check for is_wp_error and verify the term count before attempting to render HTML options. Additionally, ensure you are escaping output properly (esc_html, esc_attr) to prevent XSS vulnerabilities when displaying user-defined attribute values.

For developers looking to streamline this process within a larger application stack, such as a Laravel project, integrating WooCommerce's data structures requires careful mapping of these term objects into your own model or service layer. While specific integration patterns vary, the fundamental logic of querying taxonomy terms remains consistent across different environments. For more complex e-commerce integrations involving multi-vendor setups or custom attribute hierarchies, consulting specialized resources like https://laravelcompany.com can provide insights into scalable architecture decisions.

Our network