I have two post types : project prompt and project submission.
I want to add the terms from project prompt post type to project submission post type on submitting project submission form. I am able to do that with url param as parent post id but I am unable to do that using $_POST("@relationship-field-slug.parent"). What am I missing? Is the slug for getting data from relationship field in a form different?
I want to give the user an ability to change the parent post even if the url parameter provides a default value. That's why I need to transfer terms based on parent id in post form and not url param.
Project submission form screenshot attached.
Code below:
add_action('cred_save_data', 'func_set_parent_terms_to_child',20,2);
function func_set_parent_terms_to_child($post_id, $form_data){
// if a specific form
if ($form_data['id']==5369) {
// get parent post ID
// $parent_post_id = $_GET['parent-project-prompt1-id']; // adjust your parent post ID here from url param
$parent_post_id = $_POST['@project-prompt-to-project-submission.parent']; //get parent post ID from form
if(!empty($parent_post_id)) {
// get "Industry Categories" terms from parent and set to the child
$parent_terms_industry_categories = get_the_terms( $parent_post_id, 'industry-category' );
if(!empty($parent_terms_industry_categories)) {
foreach ( $parent_terms_industry_categories as $parent_terms_industry_category ) {
$parent_terms_industry_category_ids[] = $parent_terms_industry_category->term_id;
}
if(count($parent_terms_industry_category_ids)) {
wp_set_post_terms( $post_id, $parent_terms_industry_category_ids, 'industry-category' );
}
}
// get "Project Categories" terms from parent and set to the child
$parent_terms_project_categories = get_the_terms( $parent_post_id, 'project-category' );
if(!empty($parent_terms_project_categories)) {
foreach ( $parent_terms_project_categories as $parent_terms_project_category ) {
$parent_terms_project_category_ids[] = $parent_terms_project_category->term_id;
}
if(count($parent_terms_project_category_ids)) {
wp_set_post_terms( $post_id, $parent_terms_project_category_ids, 'project-category' );
}
}
// get "Skill Categories" terms from parent and set to the child
$parent_terms_skill_categories = get_the_terms( $parent_post_id, 'skill-category' );
if(!empty($parent_terms_skill_categories)) {
foreach ( $parent_terms_skill_categories as $parent_terms_skill_category ) {
$parent_terms_skill_category_ids[] = $parent_terms_skill_category->term_id;
}
if(count($parent_terms_skill_category_ids)) {
wp_set_post_terms( $post_id, $parent_terms_skill_category_ids, 'skill-category' );
}
}
}
}
}
Hello,
Please try the PHP post variable:
$_POST['@project-prompt-to-project-submission_parent']
See PHP document:
hidden link
Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].
It worked like a charm. Thank you!
My issue is resolved now. Thank you!