PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Can I store a list field's multiple column values into a custom taxonomy?

  1. Instead of using the Wordpress backend, I've created a form and embedded it into a page. This form will be used by contributors to submit things which will be reviewed prior to posting.

    The form will submit to a custom post type.

    One of the fields I have is a list field with multiple columns, which looks like:

    Amount | Measurement | Ingredient | Notes

    This list field is linked to my ingredient custom taxonomy. I'm having trouble with filing the list values to the custom taxonomy.

    In my Wordpress backend, I'm using the following to collect the ingredient. Note that i is used because of adding more than one ingredient:

    <input type="text" name="ingredient['.$i.'][amount]" />
    <input type="text" name="ingredient['.$i.'][measurement]" />
    <input type="text" name="ingredient['.$i.'][ingredient]" />
    <input type="text" name="ingredient['.$i.'][notes]" />

    I'm using the following to file the ingredient:

    $the_ingredients = $_POST['ingredient'];
    foreach($the_ingredients as $the_ingredient) {
    	$ingredients[] = $the_ingredient['ingredient'];
    }
    wp_set_object_terms( $post_id, $ingredients, 'ingredient' );

    Any help is greatly appreciated!

    Posted 8 years ago on Sunday December 18, 2011 | Permalink
  2. I think I see the problem. When Gravity Forms outputs the form html, it is using the input name of input_NUMBER[] where NUMBER is the number of the field on the form.

    Is there a way of defining the input name myself using a hook/filter?

    Thank you!

    Posted 8 years ago on Monday December 19, 2011 | Permalink
  3. One of our developers will take a look at this and respond shortly. As this is a customization there may be a delay in response as they will have to take a look and see the best way to handle this but non-customization related support questions take priority over customization questions. But we will take a look and respond back with some suggestions.

    Posted 8 years ago on Monday December 19, 2011 | Permalink
  4. Hi Jacore,

    You can not modify the name of the list field inputs; however, if you know the name of the GF list field input, have you considered simply using that?

    Additionally, take a look at this post for a better understanding of how the list field works:
    http://www.gravityhelp.com/forums/topic/list-field-help

    Posted 8 years ago on Tuesday December 20, 2011 | Permalink
  5. Are you saying to change the name of my custom field from ingredient to input_NUMBER?

    Posted 8 years ago on Tuesday December 20, 2011 | Permalink
  6. Couldn't the gform_field_input hook be used to modify the input name? If so, could you provide an example? It would be great if the documentation could show a few more examples to learn from. Maybe simple, moderate, and advanced examples would be helpful for those who aren't full fledged developers!

    Posted 8 years ago on Tuesday December 20, 2011 | Permalink
  7. Hi Jacorre,

    Are you saying to change the name of my custom field from ingredient to input_NUMBER?

    Yes. This is going to be the simplest solution.

    Bear in mind, although you can change the input name via the gform_field_input hook, it isn't intended to serve this purpose. The values would be posted under the updated input name; however, these values would not be processed by Gravity Forms because it would not know to look for them.

    Posted 8 years ago on Tuesday December 20, 2011 | Permalink
  8. The signed in users will use this form to submit entries which would then be reviewed. The entries will be filed to a custom post type. So I wouldn't want to change the custom field name especially since I already have values defined for other entries.

    When you say the values would not be processed by Gravity Forms, do you mean that the values would not appear under the Gravity Forms entries?

    If that's the case, it's not a problem since the submission should file to a custom post type. That's why I need to make sure I'm using the correct input name so it can file to the custom field.

    Could you provide an example as to how to create the field_input hook? I appreciate your help on this. Thank you!

    Posted 8 years ago on Tuesday December 20, 2011 | Permalink
  9. Wanted to make sure I'm understanding this! Each list field name is input_9[]. The columns I'm using are Amount, Measurement, Ingredient, Notes. If I was to unserialize that entry would the number be 9? The array for two rows of input would be:

    array (
      [0] = array
        (
          [Amount] => 1
          [Measurement] => Cup
          [Ingredient] => Sugar
          [Notes] => Some comments
        )
      [1] = array
        (
          [Amount] => 2
          [Measurement] => Cups
          [Ingredient] => Flour
          [Notes] => Some comments
        )
    )

    I need help in wrapping my mind around how I could use a hook/filter to store that into my ingredient custom field. So that the above example would file as:

    ingredient[0][amount] = 1
    ingredient[0][measurement] = Cup
    ingredient[0][ingredient] = Sugar
    ingredient[0][notes] = Some comments
    ingredient[1][amount] = 2
    ingredient[1][measurement] = Cups
    ingredient[1][ingredient] = Flour
    ingredient[1][notes] = Some comments

    I love the plugin, just can't get around this one issue at the moment!

    Posted 8 years ago on Wednesday December 21, 2011 | Permalink
  10. Hi David,

    If you or someone else could provide an example of how to create a hook/filter for changing the input field's name, it will help lead me down the right path and I'm sure I can take it from there.

    Thank you!

    Posted 8 years ago on Tuesday January 3, 2012 | Permalink
  11. Hi jacorre,

    You're on the right track with this:

    [php]
    array (
      [0] = array
        (
          [Amount] => 1
          [Measurement] => Cup
          [Ingredient] => Sugar
          [Notes] => Some comments
        )
      [1] = array
        (
          [Amount] => 2
          [Measurement] => Cups
          [Ingredient] => Flour
          [Notes] => Some comments
        )
    )

    It looks like your $ingredient properties are the same as your column names, they just need to be lower case. PHP saves the day. You can assign the unserialized array directly to your $ingredient variable and it should work perfectly with one little tweak:

    [php]
    
    // set the unserialized array to your $ingredient variable
    $ingredient = unserialize($entry[9]);
    
    // update the array keys (ie "Amount", "Ingredient") to lower case to work with your existing code
    $ingredient = array_change_key_case($ingredient, CASE_LOWER);

    Make sense?

    Posted 8 years ago on Friday January 6, 2012 | Permalink
  12. Makes sense thank you! But I'm still lost with getting it to file to my taxonomy? I've tried using the gform_field_input to change the name input but that wasn't working for me. So I tried using the gform_after_submission to file to the taxonomy but was getting a bunch of random numbers/letters in the fields. It was better than what I was getting before though! I'm close just must be missing something?

    I was using the following in my hook:

    function set_post_content($entry, $form) {
     $post = get_post($entry["post_id"]);
     $ingredients = unserialize($entry[9]);
     $ingredients = array_change_key_case($ingredients, CASE_LOWER);
     update_post_meta($entry["post_id"],'ingredient',$ingredients);
    }
    add_action("gform_after_submission_1", "set_post_content", 10, 2);
    Posted 8 years ago on Saturday January 7, 2012 | Permalink
  13. Actually I think the only problem now is that the case to lower doesn't appear to be working. Is there something wrong with the way I wrote it? I'm still seeing the column labels' first letter appearing in upper case in the database.

    Posted 8 years ago on Saturday January 7, 2012 | Permalink
  14. Ok I tested this online:

    $a=array(array("Amount"=>"1","Measurement"=>"Cup","Ingredient"=>"Sugar","Notes"=>"Some text"));
    print_r(array_change_key_case($a,CASE_LOWER));

    And it didn't work. It stays as upper case. I think it's because of an array within an array.

    So I found a function to help with that:

    function change_case_recursive($arr){
        foreach ($arr as $key=>$val){
            if (!is_array($arr[$key])){
                $arr[$key]=mb_strtolower($arr[$key]);
            }else{
                $arr[$key]=change_case_recursive($arr[$key]);
            }
        }
        return $arr;
    }

    But it's not working. And now I'm having a problem with the form not going to the thank you message after submitting. I was so close!

    Posted 8 years ago on Saturday January 7, 2012 | Permalink
  15. Fixed the issue with the thank you message after submitting.

    Just trying to understand how to make the inner array keys go to lowercase? Do you happen to have a function that would help?

    I can see that the data is filing to the database but just not with the lowercase keys. We are so close!

    Posted 8 years ago on Saturday January 7, 2012 | Permalink
  16. Sorry to keep posting but I'm not seeing what you describe as the keys/values for a multiple column list field. It appears that upon filing, the data is not serialized? So if I enter two rows of ingredients, I see the following in the database:

    1|Cup|Salt|Notes
    1|Cup|Sugar|Notes

    I'm expecting something like:

    a:5:{i:0;a:4:{s:6:"amount";s:2:"12";s:11:"measurement";s:6:"Ounces";s:10:"ingredient";s:13:"Candy Coating";s:5:"notes";s:5:"White";}i:1;a:4:{s:6:"amount";s:1:"6";s:11:"measurement";s:9:"Teaspoons";s:10:"ingredient";s:10:"Shortening";s:5:"notes";s:0:"";}i:2;a:4:{s:6:"amount";s:3:"1/4";s:11:"measurement";s:8:"Teaspoon";s:10:"ingredient";s:13:"Food Coloring";s:5:"notes";s:5:"Green";}i:3;a:4:{s:6:"amount";s:1:"4";s:11:"measurement";s:0:"";s:10:"ingredient";s:12:"Oreo Cookies";s:5:"notes";s:4:"Mint";}i:4;a:4:{s:6:"amount";s:1:"2";s:11:"measurement";s:8:"Packages";s:10:"ingredient";s:13:"Andes Candies";s:5:"notes";s:24:"4.67 ounces each package";}}

    Posted 8 years ago on Sunday January 8, 2012 | Permalink
  17. Just to recap, I removed previous tries in the functions file for trying to save stuff to the database. I am using the Custom Post Types plugin with Gravity Forms so that I can file the Ingredients field to a custom taxonomy called ingredient.

    What I'm seeing in the database is not one row of data for multiple fields:

    1|Cup|Salt|Notes
    1|Cup|Sugar|Notes

    It is one row per list field row. Is there a way to file it as one row of data for all entries of the ingredients list?

    Because that is how I'm storing it currently in the database:

    ingredient[0][amount] = 1
    ingredient[0][measurement] = Cup
    ingredient[0][ingredient] = Sugar
    ingredient[0][notes] = Some comments
    ingredient[1][amount] = 2
    ingredient[1][measurement] = Cups
    ingredient[1][ingredient] = Flour
    ingredient[1][notes] = Some comments
    Posted 8 years ago on Monday January 9, 2012 | Permalink
  18. Hi Jacore,

    Sorry buddy, I'm a little lost. Do you have Skype or Google Chat? I think we might be able to clear this one up a little faster via chat. Email me your details. :)

    Posted 8 years ago on Monday January 9, 2012 | Permalink
  19. I apologize. I've been trying to test so many different ways to see if I could get it to work. I have emailed you on your website contact form.

    Posted 8 years ago on Tuesday January 10, 2012 | Permalink
  20. Let me start from the beginning in case others need help on this as well. I was reviewing the detailed description you provided on list fields here: http://www.gravityhelp.com/forums/topic/list-field-help.

    I have a 4 column list with the following column labels:

    Column 1: Amount
    Column 2: Measurement
    Column 3: Ingredient
    Column 4: Notes

    I'm using the Gravity Forms + Custom Post Types plugin. The form is being saved to a custom post type in Pending status and this list field is saving to a custom field called ingredient.

    Based on what you explained previously about list fields and how they are saved, I was expecting something like the following in the database for the ingredient field:

    a:5:{i:0;a:4:{s:6:"amount";s:2:"12";s:11:"measurement";s:6:"Ounces";s:10:"ingredient";s:13:"Candy Coating";s:5:"notes";s:5:"White";}i:1;a:4:{s:6:"amount";s:1:"6";s:11:"measurement";s:9:"Teaspoons";s:10:"ingredient";s:10:"Shortening";s:5:"notes";s:0:"";}i:2;a:4:{s:6:"amount";s:3:"1/4";s:11:"measurement";s:8:"Teaspoon";s:10:"ingredient";s:13:"Food Coloring";s:5:"notes";s:5:"Green";}i:3;a:4:{s:6:"amount";s:1:"4";s:11:"measurement";s:0:"";s:10:"ingredient";s:12:"Oreo Cookies";s:5:"notes";s:4:"Mint";}i:4;a:4:{s:6:"amount";s:1:"2";s:11:"measurement";s:8:"Packages";s:10:"ingredient";s:13:"Andes Candies";s:5:"notes";s:24:"4.67 ounces each package";}}

    Instead, what I'm seeing in the database is:

    12|Ounces|Candy Coating|White
    6|Teaspoons|Shortening
    etc...

    It's not saving the list field with key/value pairs as one row in the database. Instead it's just saving the values and creating a row for each ingredient. [Just an fyi in case it matters, since I'm filing to a custom post type I don't need the form entry it creates, so I am using a function you provided in another forum post to delete the form entry upon submission.]

    So when I go to review the custom post type entry, it's not displaying the ingredient field data that was submitted because it's looking for:

    ingredient[i][amount]
    ingredient[i][measurement]
    ingredient[i][ingredient]
    ingredient[i][notes]

    Does that help?

    Posted 8 years ago on Tuesday January 10, 2012 | Permalink
  21. Thanks David for taking the time to work with me on this issue. From my testing, I can see that it will not be possible to get this to work.

    Once the submitted entry gets filed to the custom post type in pending status, I will then review the information.

    Two fields will not display on the post screen because I'm using jquery on those fields. The jquery is used to create new rows in the table when the plus sign is clicked. So when displaying those fields, the fields are looking for values stored in the database like I described.

    The values stored in the database when the gform is submitted isn't in the same format so that's where I run into problems.

    I may have to scrap the idea of using gravity forms altogether and try creating my own front end form instead to duplicate what I use in the admin area.

    Or just deal with the fact that two fields don't automatically file to the custom post type and use the email generated from the submission and copy the entries for those two fields in.

    Posted 8 years ago on Thursday January 12, 2012 | Permalink
  22. David, thanks again for taking the time to help the other day.

    Sad to say that I had to stop using Gravity Forms, but happy to say that I was able to create my own front end form that files all the fields perfectly and displays correctly in my admin area for my custom post type.

    I do love Gravity Forms and will probably use it for a different website of mine.

    I know it was mentioned that changing the field name is not possible due to how the form validates fields, etc. I really think that it should be included as an option. If the name can be defined by the user, I would think that the validation would first check if a name was defined and if not then use the standard name entry. Just my suggestion for future versions.

    Thanks again!

    Posted 8 years ago on Saturday January 14, 2012 | Permalink
  23. Hi Jacorre,

    Thanks for sharing your thoughts. Sorry you weren't able to use Gravity Forms in your recent project; however, I'm sure you will find many uses for it in the future. :)

    Posted 8 years ago on Wednesday February 1, 2012 | Permalink

This topic has been resolved and has been closed to new replies.