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.

Pre Render a Taxonomy Checlist, then update post

  1. I am having trouble populating a checklist with the checked values of a post, and then update the terms after submission. The post being edited is sent to the form by query string, and I did fine getting the name and values for the taxonomy in the form. However, everything I have tried for checking boxes and the setting post terms hasn't worked, wondering if anyone knows what I am doing wrong?

    Here is the pre-render with just the taxonomy and values, I tried using, wp_get_post_terms and differnt ways to get the checked values in, but couldn't

    add_filter('gform_pre_render_2', 'populate_each_product');
    function populate_each_product ($form){
    global $post, $wpdb;
    $id = $_GET['kr_id'];
    if(!empty($id)){
            foreach($form["fields"] as &$field){
                if($field["id"] == 3){
                	$args = array( 'hide_empty' => false, );
                	$cats = get_terms('product_category', $args);
                	$field['choices'] = array();
                	foreach ($cats as $cat){
    			 $name =  $cat -> name;
    			 $val =  $cat -> term_id;
            	         $field['choices'][] = array( 'text' => $name,'value' => $val);
                    }
                }
             }
        return $form;
        }
    }

    As far as post submission I have tried tons of different ways to get it to set_post_Terms but couldn't get anything to work

    add_filter('gform_post_submission_2', 'GF_update_product');
    function GF_update_product ($entry) {
    
      global $wpdb, $post;
    
      $id= $_GET['kr_id'];
    
       $entry["3"] = array();
       foreach($entry["3"]->value as $term){
          wp_set_post_terms($id, $term, 'product_category' );
       }
    
    }
    Posted 6 years ago on Friday March 29, 2013 | Permalink
  2. I think I would add some debug statements to your code to see where this is failing. We should tackle one problem at a time. How about the post_submission first, since it will be easiest?

    First of all, you should use gform_after_submission since gform_post_submission has been deprecated.

    Second, I would add some debug statements. Add "echo $id;" on line 7 and see what it contains. Dump the $entry array on line 3, like this "print_r($entry);" to see what it contains.

    Then, take a look at your code on line 8. You set $entry['3'] to a empty array, but then you try to iterate over it on line 9. That code will never do anything and will probably throw an exception since the array is empty when you try to iterate over the array items.

    Can you explain what you're trying to do with the loop which begins on line 9?

    Posted 6 years ago on Monday April 1, 2013 | Permalink
  3. Okay, I switched over to after submission

    I was able to get the pre-render to work (kind of) using in_array and isSelected. So it appears correctly, however once I did this now GF is not recognizing and submissions from the field, the entry object is always empty.

    add_filter('gform_after_submission_2', 'GF_update_product');
    function GF_update_product ($entry) {
    
      global $wpdb, $post;
    
      $thenumber = $_GET['kr_id'];
    
    	$tripdesc = array();
    	$tripdesc['ID'] =  $thenumber;
    	$tripdesc['post_content'] = $entry["2"];
    	$tripdesc['post_title'] = $entry["1"];
    	wp_update_post ( $tripdesc );
    
       __update_post_meta($thenumber, 'mp_price', $entry["4"]);
       __update_post_meta($thenumber, 'mp_shipping', $entry["5"]);
    
    	$terms = $entry["3"];
    	foreach($terms as $term){
    		wp_set_post_terms($thenumber, $term, 'product_category' );
    	}
    }
    
    add_filter('gform_pre_render_2', 'populate_each_product2');
    function populate_each_product2 ($form){
    
    global $post, $wpdb;
    
    $id = $_GET['kr_id'];
    
    if(!empty($id)){
    
            foreach($form["fields"] as &$field){
                if($field["id"] == 1){
                    $field["defaultValue"] = get_the_title($id);
                }
    
                if($field["id"] == 2){
                    $field["defaultValue"] = get_post_field('post_content', $id);
                }
    
                if($field["id"] == 4){
                    $field["defaultValue"] = get_post_meta($id, 'mp_price', TRUE);
                }
    
                if($field["id"] == 5){
                    $field["defaultValue"] = get_post_meta($id, 'mp_shipping', TRUE);
                }
    
                if($field["id"] == 3){
                	$args = array( 'hide_empty' => false, );
                	$cats = get_terms('product_category', $args);
                	$field['choices'] = array();
                	foreach ($cats as $cat){
    			$name =  $cat->name;
    			$val  =  $cat->term_id;
    			$term_list = wp_get_post_terms($id, 'product_category', array("fields" => "ids"));
    			if (in_array($val, $term_list)) { $select = true;} else { $select = false; }
    
    				$field['choices'][] = array( 'text' => $name, 'value' => $val, 'isSelected' => $select);
    
                    }
                }
    
             }
    
        return $form;
    
        }
    }
    Posted 6 years ago on Monday April 1, 2013 | Permalink
  4. Okay I was able to get everything pretty close to done. The only problem is that for wp_set_post_terms I need the $entry["3"] to be an array or csv. Any ideas how to do that?

    Posted 6 years ago on Monday April 1, 2013 | Permalink
  5. You need to make $entry['3'] an array or CSV? What does $entry['3'] contain when the form is first submitted?

    Posted 6 years ago on Monday April 1, 2013 | Permalink
  6. It is a checkbox field so it submitting numeric values for the selection

    Posted 6 years ago on Tuesday April 2, 2013 | Permalink
  7. Okay so doing it manually worked, but isn't ideal:

    $terms = $entry["3.1"].', '.$entry["3.2"].', '.$entry["3.3"].', '.$entry["3.4"].', '.$entry["3.5"].', '.$entry["3.6"].', '.$entry["3.7"].', '.$entry["3.8"].', '.$entry["3.9"].', '.$entry["3.10"].', '.$entry["3.11"].', '.$entry["3.12"].', '.$entry["3.13"].', '.$entry["3.14"].', '.$entry["3.15"];
    
       wp_set_post_terms($thenumber, $terms, 'product_category' );

    Is there anyway around this?

    Posted 6 years ago on Tuesday April 2, 2013 | Permalink
  8. Rather create a string to create a CSV of all your terms, I think I would loop through every 3.* entry, and add it to an array (I thought you mentioned previously that it could be an array or CSV). There's no need to type out every field ID and add it manually to a string like that. You could also use a loop and build the string, rather than type out each input field like that.

    Posted 6 years ago on Tuesday April 2, 2013 | Permalink