How to Retrieve Post Meta From a Variation

If you’re looking to use the data added to your variation’s custom fields elsewhere, this doc will help you retrieve the given data.

Retrieving variation meta

To retrieve a variations custom meta we first need to know what field we are looking for. 

WooCommerce Custom Fields for Variations fields slugs are formatted like so: 

iconic_cffv_[$field_id]_[$field_slug]

For example, iconic_cffv_ 177_colour_notes. To find the $field_id, look at your address bar, and find ?post. The number following is the ID number of the field group. In the example below, you can see it is 177.

Edit Variation Field Group

To get the slug of the field you’re looking to retrieve, click to edit your field, and copy the data in field ID.

Editing Field

If you know the product variation ID that you’re looking for, you can use get_post_meta to retrieve the saved value. In the example below, the product variation ID we are looking for, is 140.

$colour_notes = get_post_meta( 140, 'iconic_cffv_177_colour_notes', true );

Retrieving variation meta within a loop

The process for retrieving variation meta within a loop, is much the same – except we need to pass a dynamic variable into get_post_meta

In the example below, we are replacing our hardcoded ID of 140, with get_the_id(), which gives us the ID of the current item in our loop.

global $post;

$args =  array(
	'post_type'      => 'product_variation',
	'post_status'    => 'publish',
	'posts_per_page' => 10,
	'post_parent'	   => $post->ID,
	'meta_query'     => array( 
		array(
			'key' => 'iconic_cffv_177_colour_notes',
		) 
	),
);
		
$query = new WP_Query( $args );
	
while( $query->have_posts() ) {
	$query->the_post();

	var_dump( get_post_meta( get_the_id(), 'iconic_cffv_177_colour_notes', true ) );

}
	
wp_reset_postdata();

WooCommerce Custom Fields for Variations

Easily add custom fields to your product variations; the perfect way to display organised additional product data to your customers.

Was this helpful?

Please let us know if this article was useful. It is the best way to ensure our documentation is as helpful as possible.