Documentation

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();

Are you using WooCommerce Custom Fields for Variations yet?

Start your 14-day free trial right now and get instant access to the features described in this article.

or learn more

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.

WooCommerce Custom Fields for Variations by Iconic

WooCommerce Custom Fields for Variations

Not got the plugin yet? Get instant access right now, free for 14 days.

or learn more

Still need help?

If you haven't found what you're looking for in our documentation, please contact support.

Get Support