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.

Creating a Post within a Custom Post Type & Taxonomy

  1. So I have a custom post type called "Lesson Plans" (i.e. "lessonplans")

    Within that I have a taxonomy called "Classes / Teachers" (i.e. "class")

    The taxonomies are the teacher's display names: Jane Smith, John Doe, etc., etc.

    I have a field that auto-populates to fill in the teacher's name based on the user who is using the form (i.e. Jane or John etc.)

    How do I get that field to post into the corresponding taxonomy?

    Here is the code I have so far...

    add_action('gform_post_submission', 'gform_add_lessonplans', 10, 2);
    function gform_add_lessonplans($entry, $form){
    
        if(!rgar($entry, 'post_id'))
            return;
    
        $post_id = rgar($entry, 'post_id');
        $valid_cat_ids = get_term_ids();
        $cat_ids = array();
    
        foreach($form['fields'] as $field){
    
            if(strpos($field['cssClass'], 'add_lessonplans') === false)
                continue;
    
            foreach($entry as $key => $value){
    
                if(intval($key) != $field['id'] || !in_array($value, $valid_cat_ids))
                    continue;
    
                $cat_ids[] = (int) $value;
    
            }
    
        }
    
        wp_set_object_terms($post_id, $cat_ids, 'class', false);
    
    }
    
    function get_term_ids(){
    
        $categories = get_terms('class', 'hide_empty=0');
        $cat_ids = array();
    
        foreach($categories as $category)
            $cat_ids[] = $category->term_id;
    
        return $cat_ids;
    }
    
    function print_rr($array){
        echo '<pre>';
        print_r($array);
        echo '</pre>';
    }

    Thanks!

    Posted 7 years ago on Friday May 18, 2012 | Permalink
  2. I'm obviously putting the CSS class of "add_lessonplans" to the field which is pulling in their name.

    Posted 7 years ago on Friday May 18, 2012 | Permalink
  3. David Peralty
    Administrator

    Have you seen this third party add-on? http://wordpress.org/extend/plugins/gravity-forms-custom-post-types/

    Posted 7 years ago on Friday May 18, 2012 | Permalink
  4. Yes. I have that installed.

    I had not seen, however, that you could create dropdowns for people to choose a taxonomy from!

    Thank you. :D
    That's not exactly how I was going to do it... but it works.

    Posted 7 years ago on Friday May 18, 2012 | Permalink