Custom Field Suite Loop This Plugin
-
First of all, Takuro, thanks for this excellent plugin! Not only does it work incredibly well, but in using it, I discovered Custom Field Suite. I had no idea it existed, and I am liking it better than Advanced Custom Fields.
I created a loop in Custom Field Suite, called “Plant Ratings”, and it has two children called “Plant Rating Dates” and “Plant Rating Values”. I then added to the spreadsheet:
– cfs_plant_rating_dates
– cfs_plant_rating_valuesAs per your example CSV custom-fields.csv.
The values in the spreadsheet columns are:
– cfs_plant_rating_dates: 2014-12-05,2015-01-04,2015-01-10,2015-01-20,2015-02-12
– cfs_plant_rating_values: 4,3.5,2,4,5I’m using these fields to populate JSON to construct a Google Chart.
For some reason though, when I import the CSV, the dates and values aren’t getting placed into the custom post type post for those custom fields. Any idea what I may be doing wrong?
Thanks!
-
To import to custom field loop, you have to create associative array with using a filter hook.
http://customfieldsuite.com/docs/save/
https://ww.wp.xz.cn/plugins/really-simple-csv-importer/other_notes/Please check out my blog post.
http://notnil-creative.com/blog/archives/2913I said about ACF in this post, but the way is same also with CFS.
Thanks Takuro.
foreach ($meta as $key => $value) { // If custom field name is "plant_rating" if ($key == 'cfs_plant_rating_values') { $repeater_array[0]['plant_rating_values'] = $value; } elseif ($key == 'cfs_plant_rating_dates') { $repeater_array[1]['plant_rating_dates'] = $value; } } // end foreach loop // Insert Repeater data $meta_array['cfs_plant_rating'] = $repeater_array;Should I be using the cfs_ prefix?
Hm, having trouble getting it to work. In your example at http://notnil-creative.com/blog/archives/2913 the repeater is made up of multiple columns, while mine is made of two columns with comma-separated values in a cell per post. Having some trouble wrapping my head around how to break it apart. I assume I need to use something similar to the ‘select’ example to break the comma-separated values apart, and then recombine them?
Using your debug add-on, after uploading CSV I’m getting:
array(2) { ["custom_field"]=> string(0) "" ["plant_rating"]=> array(2) { [0]=> array(1) { ["plant_rating_dates"]=> string(54) "2014-12-05,2015-01-04,2015-01-10,2015-01-20,2015-02-12" } [1]=> array(1) { ["plant_rating_values"]=> string(11) "4,3.5,2,4,5" } } }When, if I manually add the rows in CFS and print_r, it looks like:
Array ( [0] => Array ( [plant_rating_values] => 4 [plant_rating_dates] => 2014-12-05 ) [1] => Array ( [plant_rating_values] => 3.5 [plant_rating_dates] => 2015-01-04 ) [2] => Array ( [plant_rating_values] => 2 [plant_rating_dates] => 2015-01-10 ) [3] => Array ( [plant_rating_values] => 2015-01-20 [plant_rating_dates] => 4 ) [4] => Array ( [plant_rating_values] => 2015-02-12 [plant_rating_dates] => 5 ) )I’m still learning PHP… arrays confuse me.
Okey, plz share your code
I’m at home now and don’t have access, but this is from memory using your code (placed in my theme’s functions.php):
function rsci_meta_filter( $meta, $post, $is_update ) { // Create containers $meta_array = array(); $repeater_array = array(); foreach ($meta as $key => $value) { // if key matches cfs_plant_rating_values column in CSV if ($key == 'cfs_plant_rating_values') { $repeater_array[0]['plant_rating_values'] = $value; // actual CFS field is plant_rating_values } elseif ($key == 'cfs_plant_rating_dates') { $repeater_array[1]['plant_rating_dates'] = $value; } else { // Pass through normal (not CFS) custom field data $meta_array[$key] = $value; } } // Insert Repeater data $meta_array['plant_rating'] = $repeater_array; return $meta_array; } add_filter( 'really_simple_csv_importer_save_meta', 'rsci_meta_filter', 10, 3 );Thanks so much for your help.
This code is not yet tested, but here is my idea:
function rsci_meta_filter( $meta, $post, $is_update ) { $meta_array = array(); $plant_ratings = array(); $values = explode(',', $meta['plant_rating_values']); $dates = explode(',', $meta['plant_rating_dates']); foreach ($values as $i => $value) { $plant_ratings['plant_rating_values'] = $value; $plant_ratings['plant_rating_dates'] = $dates[$i]; } $meta_array['cfs_plant_ratings'] = $plant_ratings; return $meta_array; } add_filter( 'really_simple_csv_importer_save_meta', 'rsci_meta_filter', 10, 3 );Hi Takuro, sorry for the delayed response. Work was canceled two days in a row due to weather.
I’ve tried your code above, but unfortunately it didn’t work. When I import the CSV, all I see is this.
My CSV is here, if this might help?
If I turn on the debugger and upload, I get this:
$is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "68" ["post_name"]=> string(27) "lobularia-blushing-princess" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-12 19:48:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(29) "Lobularia 'Blushing Princess'" ["post_content"]=> string(139) "Sprayed 3 times to control spider mite damage. This is a test on Lobularia. Does it update from the spreadsheet? Fertilized once a month." } $meta: array(1) { ["cfs_plant_ratings"]=> array(2) { ["plant_rating_values"]=> string(1) "5" ["plant_rating_dates"]=> string(10) "2014-06-12" } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(14) "Proven Winners" } } $is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "71" ["post_name"]=> string(8) "marigold" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-20 20:02:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(8) "Marigold" ["post_content"]=> string(74) "Sprayed 3 times to control spider mite damage. Fertilized once a month." } $meta: array(1) { ["cfs_plant_ratings"]=> array(2) { ["plant_rating_values"]=> string(3) "3.5" ["plant_rating_dates"]=> string(10) "2014-12-29" } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(4) "Ball" } } $is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "73" ["post_name"]=> string(5) "vinca" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-20 20:13:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(5) "Vinca" ["post_content"]=> string(74) "Sprayed 3 times to control spider mite damage. Fertilized once a month." } $meta: array(1) { ["cfs_plant_ratings"]=> array(2) { ["plant_rating_values"]=> string(3) "3.5" ["plant_rating_dates"]=> string(10) "2014-05-11" } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(6) "Sakata" } } $is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "75" ["post_name"]=> string(7) "petunia" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-21 20:13:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(7) "Petunia" ["post_content"]=> string(90) "Sprayed 3 times to control spider mite damage. This is Petunia. Fertilized once a month." } $meta: array(1) { ["cfs_plant_ratings"]=> array(2) { ["plant_rating_values"]=> string(1) "5" ["plant_rating_dates"]=> string(10) "2015-02-12" } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(5) "Takii" } } All Done.Even though it just says “Import CSV 1.” when I disable the debug plugin.
Think I’m closer!
function rsci_meta_filter( $meta, $post, $is_update ) { $meta_array = array(); $plant_ratings = array(); $values = explode(',', $meta['cfs_plant_rating_values']); $dates = explode(',', $meta['cfs_plant_rating_dates']); $ratings_data = array_combine($dates, $values); foreach ($ratings_data as $ratings_key => $ratings_value) { $plant_ratings['plant_rating_dates'][] = $ratings_key; $plant_ratings['plant_rating_values'][] = $ratings_value; } $meta_array['plant_ratings'] = $plant_ratings; return $meta_array; } add_filter( 'really_simple_csv_importer_save_meta', 'rsci_meta_filter', 10, 3 );This seems to work in the debugger, but when I disable the debugger plugin and run it, nothing gets saved to the posts. It doesn’t do the “Import CSV 1.” thing though.
This is what appears in the debugger:
$is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "68" ["post_name"]=> string(27) "lobularia-blushing-princess" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-12 19:48:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(29) "Lobularia 'Blushing Princess'" ["post_content"]=> string(139) "Sprayed 3 times to control spider mite damage. This is a test on Lobularia. Does it update from the spreadsheet? Fertilized once a month." } $meta: array(1) { ["plant_ratings"]=> array(2) { ["plant_rating_dates"]=> array(2) { [0]=> string(10) "2014-04-12" [1]=> string(10) "2014-06-12" } ["plant_rating_values"]=> array(2) { [0]=> string(3) "3.5" [1]=> string(1) "5" } } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(14) "Proven Winners" } } $is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "71" ["post_name"]=> string(8) "marigold" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-20 20:02:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(8) "Marigold" ["post_content"]=> string(74) "Sprayed 3 times to control spider mite damage. Fertilized once a month." } $meta: array(1) { ["plant_ratings"]=> array(2) { ["plant_rating_dates"]=> array(2) { [0]=> string(10) "2014-12-24" [1]=> string(10) "2014-12-29" } ["plant_rating_values"]=> array(2) { [0]=> string(1) "4" [1]=> string(3) "3.5" } } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(4) "Ball" } } $is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "73" ["post_name"]=> string(5) "vinca" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-20 20:13:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(5) "Vinca" ["post_content"]=> string(74) "Sprayed 3 times to control spider mite damage. Fertilized once a month." } $meta: array(1) { ["plant_ratings"]=> array(2) { ["plant_rating_dates"]=> array(2) { [0]=> string(10) "2014-03-12" [1]=> string(10) "2014-05-11" } ["plant_rating_values"]=> array(2) { [0]=> string(1) "4" [1]=> string(3) "3.5" } } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(6) "Sakata" } } $is_update: bool(true) $post: array(8) { ["post_type"]=> string(5) "plant" ["ID"]=> string(2) "75" ["post_name"]=> string(7) "petunia" ["post_author"]=> int(41) ["post_date"]=> string(19) "2015-01-21 20:13:00" ["post_status"]=> string(7) "publish" ["post_title"]=> string(7) "Petunia" ["post_content"]=> string(90) "Sprayed 3 times to control spider mite damage. This is Petunia. Fertilized once a month." } $meta: array(1) { ["plant_ratings"]=> array(2) { ["plant_rating_dates"]=> array(5) { [0]=> string(10) "2014-12-05" [1]=> string(10) "2015-01-04" [2]=> string(10) "2015-01-10" [3]=> string(10) "2015-01-20" [4]=> string(10) "2015-02-12" } ["plant_rating_values"]=> array(5) { [0]=> string(1) "4" [1]=> string(3) "3.5" [2]=> string(1) "2" [3]=> string(1) "4" [4]=> string(1) "5" } } } $tax: array(1) { ["company"]=> array(1) { [0]=> string(5) "Takii" } }Success!
I got one of my programmer co-workers to step in and help me arrange the PHP array properly. Also, stupidly, I was naming the meta_array wrong – it should have been (cfs_)plant_rating, not (cfs_)plant_ratings, which is why it wasn’t saving. -_- (I had originally called it plant_ratings when I was using Advanced Custom Fields, but I switched to Custom Field Suite, and got rid of the s, but forgot)
Here is the final code:
function rsci_meta_filter( $meta, $post, $is_update ) { // Arrays to store data $meta_array = array(); $plant_ratings = array(); // Separate the fields at the comma $values = explode(',', $meta['cfs_plant_rating_values']); // imports the comma-separated ratings $dates = explode(',', $meta['cfs_plant_rating_dates']); // imports the comma-separated dates $c = 0; foreach ($dates as $key => $val){ $tmp = array(); $tmp['plant_rating_values'] = $values[$c]; $tmp['plant_rating_dates'] = $dates[$c]; $plant_ratings[] = $tmp; $c++; } // Insert repeater data $meta_array['cfs_plant_rating'] = $plant_ratings; return $meta_array; } add_filter( 'really_simple_csv_importer_save_meta', 'rsci_meta_filter', 10, 3 );
The topic ‘Custom Field Suite Loop This Plugin’ is closed to new replies.