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.

Create new category or add to existing

  1. Hello,

    I have a category dropdown on my form: State > Base. The user selects their base using this dropdown. In a separate text field, they write the name of their neighborhood. When the form is submitted, I want to take the neighborhood text and turn it into a category. So the post created by the form will be saved under: State > Base > Neighborhoods > Neighborhood Name. If the neighborhood already exists then the post will be saved in that category.

    I think I need to use gform_post_data to accomplish this but it's not working. This is what I have so far:

    add_filter('gform_post_data_6', 'update_cat');
    function update_cat($post_data){
    
        $post_data['post_category'] .= 'neighborhood';
    
        return $post_data;
    }

    Any tips?

    Posted 12 years ago on Saturday July 16, 2011 | Permalink
  2. Hi Caseym,

    The post_category property of the $post_data array is an array of IDs this post is assigned to. In order to add new categories to the post data you will need to a) retrieve the category's term ID and b) assign it as a new index in the existing array.

    [php]
    $post_data['post_category'][] = 14;

    In regards to creating and assigning the post to a new category if the entered neighborhood does not exist, here are two functions that might be useful to you.

    1. get_term_by
      This will allow you to search for a term by the entered name.
    2. wp_insert_term
      If the term does not exist, you can use this function to create it. It will return the term ID so you can assign it to the post category array.
    Posted 12 years ago on Saturday July 16, 2011 | Permalink
  3. Thanks David, this worked perfectly!

    Casey

    Posted 12 years ago on Sunday July 17, 2011 | Permalink

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