Hi Dave,
I used the following code and it works great! Thanks for your help/pointers!!!
// select parent term automatically for the 'project_type' taxonomy in form #1
add_action('gform_after_submission_1', 'gform_parent_category');
function gform_parent_category($entry) {
$created_post = $entry['post_id'];
$child_ID = $entry["7"];
$term_object = get_term_by('id', $child_ID, 'project_type'); // replace project_type by chosen taxonomy
$parent_ID = $term_object -> parent;
wp_set_post_terms( $created_post, $parent_ID, 'project_type', true ); //replace project_type by chosen taxonomy
}
But instead of copying the code for each form, I wanted a flexible solution: I tried to use wp_get_post_terms instead. The only problem is that function returns an empty string! No idea why?!? Quite messy code as I tried different options:
add_action('gform_after_submission', 'gform_parent_category');
function gform_parent_category($entry) {
$created_post = $entry['post_id'];
$post = get_post($entry["post_id"]);
//$child_ID = $entry["7"];
//$term_object = get_term_by('id', $child_ID, 'project_type'); // replace project_type by chosen taxonomy
$query_taxonomy = 'project_type';
$term_object = wp_get_post_terms($created_post, $query_taxonomy, array("fields" => "all")); // replace project_type by chosen taxonomy
$product_terms = wp_get_object_terms($created_post, 'project_type', array("fields" => "all"));
$parent_ID = $term_object -> parent;
print_r($term_object);
var_dump($term_object);
var_dump($product_terms);
var_dump($post);
//$parent_ID = $term_object[parent];
echo $parent_ID;
wp_set_post_terms( $created_post, $parent_ID, 'project_type', true ); //replace project_type by chosen taxonomy
}
Have you got any clues?
Posted 12 years ago on Saturday December 3, 2011 |
Permalink