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.

custom taxonomies

  1. obvio
    Member

    Hi all,
    I'm trying some custom taxonomies to be inserted when creating a post (user generated content on a gf form), and I'm stuck on how to get the values for any field before submitting the post to the database.
    can anyone shed some light on that (docs are kinda lacking the hooks/filters and the more advanced stuff..).
    Thanks!

    Posted 9 years ago on Monday April 19, 2010 | Permalink
  2. You would have to use a Post Tag field for the form field and then write some custom PHP to take advantage of an API hook to tell that field to store as a custom taxonomy. I'll have our lead developer chime in with a code snippet.

    Posted 9 years ago on Tuesday April 20, 2010 | Permalink
  3. Try the following, replacing "post_tag" with your custom taxonomy.

    add_filter("gform_post_data", "custom_taxonomy", 10, 2);
    function custom_taxonomy($post_data, $form){
    	//-----------------------------------------
    	//Replace "post_tag" with your taxonomy name
    	$custom_taxonomy = "post_tag";
    	//----------------------------------------
    
    	$tags = implode(",", $post_data["tags_input"]);
    	$post_data["tax_input"] = array($custom_taxonomy => $tags);
    	$post_data["tags_input"] = null;
    	return $post_data;
    }
    Posted 9 years ago on Tuesday April 20, 2010 | Permalink
  4. obvio
    Member

    thanks, it works great, but in my case i have 3 custom taxonomies.. so adding another tag box always overwrites the previous one, also i do 3 code blocks inside the function with 3 sets of variables to accommodate al taxonomies it doesn't work for me.

    what I've found "wp_set_object_terms()" but it requires the post ID which I get only _after_ submitting it to the db.. (in "gform_post_submission" hook).

    can u show me a way to preserve the value of some field to after the post was submitted or any other take on your version to accommodate more then one taxonomy?

    thanks!

    Posted 9 years ago on Tuesday April 20, 2010 | Permalink
  5. You are right. It doesn't like the multiple tag fields. We will have to get a little creative to get around the problem. The first hook prevents the tags from being inserted into the post tags. The second hook inserts them into the custom taxonomies using the wp function you found. Let me know how it goes

    add_filter("gform_post_data", "empty_tags", 10, 2);
    function empty_tags($post_data, $form){
        //replace 4 with your actual form id
        if($form["id"] !=4)
            return;
    
        $post_data["tags_input"] = null;
        return $post_data;
    }
    
    add_filter("gform_post_submission", "insert_custom_taxonomies", 10, 2);
    function insert_custom_taxonomies($entry, $form){
    
        //REPLACE 4 with your actual form id
        if($form["id"] !=4)
            return;
    
        //getting post
        $post_id = $entry["post_id"];
    
        //REPLACE 2 with your actual field id and "post_tag" with your custom taxonomy
        wp_set_object_terms($post_id, explode(",", $entry[2]), "post_tag");
    
        //REPLACE 3 with your actual field id and "activities" with your second taxonomy
        wp_set_object_terms($post_id, explode(",", $entry[3]), "activities");
    }
    Posted 9 years ago on Wednesday April 21, 2010 | Permalink
  6. obvio
    Member

    thanks! I stayed late yesterday and hacked it slightly different, I used a few single line field instead of the tags-field, i think it's conceptually more correct and saves up on one function call!

    In retrospective, It will be really really beneficial if you could describe the life cycle of the form and the structure of the object created when first rendering it to the page and through its sending phase, another thing helpful would be explaining where each hook/filter can intervene.

    Posted 9 years ago on Wednesday April 21, 2010 | Permalink
  7. Good call on not using the tag field. It does save you that function call. Better documentation for hooks and objects are coming soon.

    Posted 9 years ago on Thursday April 22, 2010 | Permalink
  8. Obvio, can you share the modifications that you made to display custom post taxonomies with a GF?

    Posted 9 years ago on Tuesday May 18, 2010 | Permalink
  9. gargleblaster
    Member

    What's the difference between gform_post_submission and gform_post_data? http://www.gravityhelp.com/documentation/hooks-and-filters/ doesn't even mention gform_post_data.

    Posted 9 years ago on Monday December 20, 2010 | Permalink
  10. The gform_post_submission hook is the post submission hook for the form itself.

    The gform_post_data is specific to Post Fields that created WordPress Posts and allow you to manipulate data before Gravity Forms created a WordPress Post.

    To the term Post has two different meanings here. In gform_post_submission hook it is post as in "After" and in gform_post_data it is post as in WordPress Post.

    Posted 9 years ago on Monday December 20, 2010 | Permalink
  11. stueynet
    Member

    Could you guys post the latest correct method for changing the post type on a submission form? So if I have a custom post type called questions and I would like users to submit new questions and have it post as a question rather than a post. I cannot fin this in the documentation and with the increased importance of custom post types in WP it would seem to be an important feature.

    Thanks!

    Posted 9 years ago on Thursday January 20, 2011 | Permalink
  12. Hi Stueynet, this should do the trick: http://pastie.org/1481054

    Posted 9 years ago on Thursday January 20, 2011 | Permalink
  13. stueynet
    Member

    Ah nice. Thanks David. Gravity Forms rules. You should add that to the documentation as well.

    Posted 9 years ago on Thursday January 20, 2011 | Permalink
  14. Agreed! We actually have some new documentation coming out with the official 1.5 release which includes all sorts of goodies like this. :)

    Posted 9 years ago on Thursday January 20, 2011 | Permalink
  15. I have two custom taxonomies. I used Alex's code from above http://www.gravityhelp.com/forums/topic/custom-taxonomies#post-4823

    This works great for the first custom taxonomy but will not populate the second custom taxonomy. The first field associated with a custom taxonomy is a drop-down list, the second field has checkboxes, so multiple values can be selected. I think the problem with not being able to populate the second taxonomy's values is because of the checkboxes.

    Any ideas of how to have checkbox values map to the custom taxonomy the same way the default category mapping works?

    Posted 7 years ago on Wednesday January 30, 2013 | Permalink
  16. I'll ask Alex for his advice on this one. Checkboxes are surprisingly difficult to populate. See this response from Alex earlier today: http://www.gravityhelp.com/forums/topic/unable-to-save-selected-values-from-checkboxes-field-in-database#post-135458

    Posted 7 years ago on Wednesday January 30, 2013 | Permalink
  17. Hi @wordpressgirl, can you please export your form as XML (Forms > Import/Export > top center menu item "Export Forms") and email that to me. Along with a complete copy of the code you are using (your whole functions.php is fine.) Attach both those to an email and send it to chris@rocketgenius.com and I will have Alex take a closer look for you.

    Posted 7 years ago on Wednesday January 30, 2013 | Permalink