Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

wp_set_post_terms from a form WordPress

  • SOLVED

I am using gf as a frontend editor and can't seem to get it to pre-render or update a post's custom taxonomies. The custom taxonomy is 'product_category'

I was able to actually output the tax's with this:

add_filter('gform_pre_render_2', 'populate_each_product');
function populate_each_product ($form){

global $post, $wpdb;

$id = $_GET['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;

}
}


But that doesn't set which is checked

Also I need some way to set the ones that are checked after submission, this is what I tried

add_filter('gform_post_submission_2', 'GF_update_product');
function GF_update_product ($entry) {

global $wpdb, $post;

$id= $_GET['id'];

$entry["3"] = array();
foreach($entry["3"] as $term){
wp_set_post_terms($id, $term, 'product_category' );
}

}

Answers (2)

2013-03-29

Christianto answers:

Hello,

To display all term on 'product_category' taxonomy with some term selected by default,
Have you try to set 'isSelected' to true for each field that match certain term/taxonomy?

for example if you want 'product_one' to be selected by default:
add_filter('gform_pre_render_2', 'populate_each_product');

function populate_each_product ($form){
global $post, $wpdb;
$id = $_GET['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;
if($name == 'product_one'){
$field['choices'][] = array( 'text' => $name,'value' => $val, 'isSelected' => true);
} else {
$field['choices'][] = array( 'text' => $name,'value' => $val);
}
}
}
}
return $form;
}
}



For some field checked after submission,
please try use 'gform_after_submission' hook instead of deprecated 'gform_post_submission'.

I wonder what is $id on $_GET['id']?
is it post id?
if true, please try to get post ID from $entry object, and get all user submitted taxonomies from $entry["3"]

add_action('gform_after_submission_2', 'GF_update_product', 10, 2);

function GF_update_product ($entry, $form){
global $wpdb, $post;
$id = $entry["post_id"];
$terms = $entry["3"];
foreach($terms as $term){
wp_set_post_terms($id, $term, 'product_category' );
}
}


Let us know if this not working..


Kyle comments:

Sorry for the slow reply, I am implementing now.

As for the bottom, this is being used to edit posts on the frontend, not create them so I can't use the $entry object to get the post_id, it has to be through query strings.

For the isselected, would I have to run through an If statement for every term manually then?


Kyle comments:

Would there be someway to use wp_get_post_terms and then do isselected if any of the term_ids = value?


Kyle comments:

I tried using in_array, but didn't get a result

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;

}
}


Kyle comments:

Oh, that pre-render does work I just needed to fix 'true' and 'false' to be true and false.


Kyle comments:

For whatever reason, I am not getting any $entry["3"] value from the field when I am using pre-render. I can see that the numeric values are there, but nothing is being submitted with the entry object..


Kyle comments:

Okay moving along, now the $entry is working, but the foreach in the after submission is turning an invalid argument supplied in the foreach error


Christianto comments:

what is the output of $entry?
I think the entry populate on individual $entry['ID'] per input field, and isn't combine as an array..
for example $entry["3.1"], $entry["3.2"], $entry["3.3"]
please check this
add_filter('gform_post_submission_2', 'GF_update_product');
function GF_update_product ($entry, $form) {
global $wpdb, $post;
$id= $_GET['id'];
foreach($form["fields"] as $field){
if($field["id"] == 3){
$all_terms = $field['choices'];
for($i = 1; $i <= count($all_terms); $i++){
$submitted_term = $entry["3.$i"];
if(!empty($submitted_term))
wp_set_post_terms($id, $submitted_term, 'product_category' );
}
}
}
}


Kyle comments:

Okay yes, I tried with the 3.1, 3.2 etc.. and that worked

With the above code though I am getting two errors, missing argument 2 and invalid argument supplied for foreach

This is current code, which works, but is sloppy because it doesn't use $i

add_filter('gform_after_submission_2', 'GF_update_product');
function GF_update_product ($entry, $form) {

global $wpdb, $post;

$id= $_GET['id'];


$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"].', id3.13"].', '.$entry["3.14"].', '.$entry["3.15"];

wp_set_post_terms($id, $terms, 'product_category' );

}


Christianto comments:

Sorry, I'm using wrong filter/hook, ;)

Please change
add_filter('gform_post_submission_2', 'GF_update_product');

add_action("gform_after_submission", "GF_update_product", 10, 2);


Christianto comments:

Sorry, I'm using wrong filter/hook, ;)

Please change
add_filter('gform_post_submission_2', 'GF_update_product');
to
add_action("gform_after_submission_2", "GF_update_product", 10, 2);


Kyle comments:

Worked perfectly, thanks for your help!

2013-03-29

Arnav Joy answers:

can you check what is output of echo $term here

foreach($entry["3"] as $term){
echo $term;
wp_set_post_terms($id, $term, 'product_category' );

}



Kyle comments:

Okay, so I have progressed and that one point is all that is left

After looking of wp_set_post_terms I realized it accepts arrays or csv's

So I tried boiling it down to

$terms = $entry["3"];
wp_set_post_terms($thenumber, $terms, 'product_category' );


But that erased all the values for the category.

When I echo $terms there is no output