Skip Navigation

[Resolved] How to update custom field value from CSV?

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

This topic contains 8 replies, has 2 voices.

Last updated by Beda 3 years, 10 months ago.

Assigned support staff: Beda.

Author
Posts
#1220714

Hi,

I need to update in bulk value of custom fields value for woocommerce products.

I have some custom fields for woocommerce products. I need to update the value for a custom field in all product based on another custom field.

Lets assume there is two custom fields with name SIte-URL and Site-data.

I need to update site-data field for every product that match to site-url. Then CSV must be like this

SIte-URL | Site-data
SIte-URL | Site-data
SIte-URL | Site-data
SIte-URL | Site-data
SIte-URL | Site-data

How could we achieve this.

#1220829

We have an extensive Documentation with in total 3 different approaches about CSV import to WordPress:
https://toolset.com/documentation/user-guides/how-to-import-content-into-wordpress-using-csv/

But, the task you want to achieve is rather related to the Import Plugin used and not Toolset.
Toolset's custom fields are simple WordPress fields in this case and the only special about them is, that their slug will have a wpcf- prefix.
That is all you need to know in difference to importing or updating data in WordPress with a CSV import.

I probably would rather go for a custom script that once loaded, gets all the Products, their Custom Fields values, and updates a new Custom Field with this value.

That can be done with get_posts(), get_post_meta() and update_post_meta() hooks of native WordPress API, where again the only to know in difference to other Custom Fields is, that Toolset Types Custom Fields are prefixed with wpcf-

If you require help with such Custom Coding I could write a small example, for larger code or assistance you'd need to contact a Developer from https://toolset.com/contractors/

#1220844

Thank you Beda,

"That can be done with get_posts(), get_post_meta() and update_post_meta() hooks of native WordPress API, where again the only to know in difference to other Custom Fields is, that Toolset Types Custom Fields are prefixed with wpcf-"

I don't need to update data from php, I need to update data form CSV file, because it needs to be update in bulk.

#1220847

And second plugin mentioned here

https://toolset.com/documentation/user-guides/how-to-import-content-into-wordpress-using-csv/

"CSV Importer plugin" is not working at all, and its not updated from last four years, so i think it must be removed from help page.

#1220859

I understand. This is a method to update data in bulk, using PHP

Using or providing CSV import is not something Toolset does.
We provide documentation on how to import Toolset specific data with other plugins not made by Toolset.
Specific data is, for example, a relationship, or a field

We elaborate on what is important to know and how to import such data in the DOC linked above.
It is important that your CSV is addressing the right Field Slug, so it is important to know that each Toolset Field has a prefix of wpcf-
This means your CSV will have to address field slugs like "wpcf-this_is_the_slug_of_the_field"

We can however not go much further than that, since CSV Import itself is not handled by the Toolset, rather by those 3rd party plugins.

#1221324

Thank you for your reply.

Could you please provide some sample code in php for updating custom field value in bulk?

I will be very thankful to you.

#1221440

For example, this post holds such an example code:
https://toolset.com/forums/topic/ajax-filter-not-working-properly/page/6/#post-620904

The File there (hidden link) applies to a very specific case but is well commented, allowing you to craft your own version of a code that gets certain posts with values and updates them with another value.

The basics:

//get all post of kind
$args = array(
	'posts_per_page'   => 5,//change amount
	'offset'           => 0,
	'cat'         => '',
	'category_name'    => '',
	'orderby'          => 'date',
	'order'            => 'DESC',
	'include'          => '',
	'exclude'          => '',
	'meta_key'         => 'wpcf-your-key',
	'meta_value'       => 'your value',
	'post_type'        => 'post',//change post type
	'post_mime_type'   => '',
	'post_parent'      => '',
	'author'	   => '',
	'author_name'	   => '',
	'post_status'      => 'publish',
	'suppress_filters' => true,
	'fields'           => '',
);
$posts_array = get_posts( $args ); 

//if any posts
if $posts_array {
  //then loop over each of those posts and do something
  foreach ($posts_array as $post){
    //get post ID
    $post_id = $post->ID;
    
    //get existing meta
    $meta = get_post_meta($post_id, 'wpcf-your-key', true);
    //update post meta
    update_post_meta($post_id, 'wpcf-your-field_new', $meta);
    }
}

I hope this helps to get started (the code above is not ready to use, it is an example)

#1221480

This code will update the all post's custom fields with same value. But i need to update all post with different value for every post's custom field.

like

post 1 -> value 1 for custom field
post 2 -> value 2 for custom field
post 3 -> value 3 for custom field

And in this code, how could we identify that which value will be updated in which post's custom field?

#1221496

This is subject to PHP custom code.

You would need to put in some IF statement, for example.

Let's say, you say "if the value is xy then update with zw"

if $posts_array {
  //then loop over each of those posts and do something
  foreach ($posts_array as $post){
    //get post ID
    $post_id = $post->ID;
     
    //get existing meta
    $meta = get_post_meta($post_id, 'wpcf-your-key', true);
    //update post meta
    if ($meta == 'xy'){
      update_post_meta($post_id, 'wpcf-your-field_new', $meta);
    }
    }
}

Or you could check upon the Post ID. For example, you could check the post ID or name and update the fields accordingly.
Of course, since you have another value for each post this also requires an adequate PHP check to be sure you are on the right post.
This is not any different in a CSV file where you will have to file and write each and every single entry by hand.