Skip Navigation

[Resolved] Add terms from related post to new post using Forms API

This thread is resolved. Here is a description of the problem and solution.

Problem: I have two custom post types in a many-to-many relationship. I would like to use Forms to create one of the custom posts and use the Forms API to connect it to an existing post. I would like to copy all the terms associated with the existing post into the new post using the Forms API.

Solution: To get the terms from the Job post, you need the Job post ID. Once you have the Job post ID, you can use wp_get_object_terms as described here: https://codex.wordpress.org/Function_Reference/wp_get_object_terms

Then to set the terms on the new post, you can use wp_set_object_terms as described here: https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 5 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 2.34 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 11 replies, has 2 voices.

Last updated by martinH-10 5 years, 4 months ago.

Assisted by: Christian Cox.

Author
Posts
#1265445
admin.png

Hi, I am building big Job site and I am lost in the forms :(.

I have a form which add the Post type Job. I have a form which adds the Post type Applicant.

Job can have many applicants. Applicant can apply form many jobs. So thats a many to many relationship.

Now I need to Add the parent post Taxonomies to form wich Adds the applicant. Form is here: hidden link - How can I do that in post form?

I also need to add relationship to the parent post when someone save the form. - - How can I do that in post form?

Then I need add job selection procedure custom fields for every Applicant in the relationship Job if they were selected succesfully or not. - How is it possible with Post form or relationship form? Is this i relationship field?

I am sending screenshot of my Administration for imagination

#1265679

Now I need to Add the parent post Taxonomies to form wich Adds the applicant. Form is here: hidden link - How can I do that in post form?
I don't understand, do you want to display the parent post's taxonomy terms, or modify the parent post's taxonomy terms?

I also need to add relationship to the parent post when someone save the form. - - How can I do that in post form?
The recommended approach with a many-to-many post relationship is to create a separate Relationship Form to manage relationships between the posts. https://toolset.com/documentation/post-relationships/how-to-build-front-end-forms-for-connecting-posts/
Any other approach will require custom code, and the Post Relationships API.

Then I need add job selection procedure custom fields for every Applicant in the relationship Job if they were selected succesfully or not. - How is it possible with Post form or relationship form? Is this i relationship field?
Sorry I dont understand what this means. Can you explain in more detail?

#1266403

Hi, lets begin slowly.

The 1st thing is, that I will publish the Post with some taxonomies.

Post is Job offer and taxonomies are Locality, Field of work etc.

Then visitor come to website, use filtering to find his "dream job" and apply for it with Post Form displayed in Job offer page.
for example: hidden link

This form is used to save visitor as a job seeker (which is a second Post Type).

In this moment we want from job seeker this fields: Name, Email, Phone, Message. But when he send the post form, we also need to save current Job offer post taxonomies into the new Job seeker post.

Also, we need to save relationship between new Job Seeker & Job Offer.

Is it clear now? And is it possible?

#1266453

For the relationship I tought on using something like this:

else if ($form_data['id']==28) {
    $postid = get_the_ID();
     toolset_connect_posts( 'zadatel-pracovni-nabidka', 5, $postid );
}

But the problem is that I know only the parent posts ID but I dont know which ID will have the new saved post. Am I true?

#1266681

But the problem is that I know only the parent posts ID but I dont know which ID will have the new saved post.
Your relationships API code looks okay. In any cred_save_data callback, you have access to the new post's ID in the first callback parameter:

add_action('cred_save_data', 'your_callback_function',100,2);
  
function your_callback_function($post_id, $form_data) {
  // $post_id is the ID of the new post
}

we also need to save current Job offer post taxonomies into the new Job seeker post.
To get the terms from the Job post, you need the Job post ID. Once you have the Job post ID, you can use wp_get_object_terms as described here: https://codex.wordpress.org/Function_Reference/wp_get_object_terms

Then to set the terms on the new post, you can use wp_set_object_terms as described here: https://codex.wordpress.org/Function_Reference/wp_set_object_terms

#1269291

ok, i updated my functions (I already have got some functions inside):

add_action('cred_save_data','func_custom_post_title',10,2);
function func_custom_post_title($post_id,$form_data) {
    if ($form_data['id']==28) {
        $name = get_post_meta($post_id, 'wpcf-name', true);
        $title= $name;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
        toolset_connect_posts( 'zadatel-pracovni-nabidka', $postid, $parentpostid );
    }
    else if ($form_data['id']==216) {
        $email = get_post_meta($post_id, 'wpcf-email-agent', true);
        $title= $email;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

But I still do not know, how to get the parent post id. If the first parameter is iD of new created post, how to get the second parameter - the ID of post where is the form?

#1269311

I am sorry, cause I am not a programmer, but I am trying to figure it out by myself and learn a new skills...

it looks like the argument

container_id

is the Job Posts ID
and argument

post_id

is the New created Posts ID

if I am true, then I can use:

toolset_connect_posts( 'zadatel-pracovni-nabidka', $form_data['container_id'], $post_id );

for creating the relationship, right?

#1269337

Ok, connecting between posts works great! Cool!!

Now the taxonomies - Do I need the function for every taxonomy? I have prepared this:

        $joblocality = wp_get_object_terms($form_data['container_id'], 'lokalita');
        $jobtype = wp_get_object_terms($form_data['container_id'], 'obor');
        $jobtime = wp_get_object_terms($form_data['container_id'], 'uvazek');
        $jobtypeger = wp_get_object_terms($form_data['container_id'], 'remeslo');

        wp_set_object_terms($post_id, $joblocality, true);
        wp_set_object_terms($post_id, $jobtype, true);
        wp_set_object_terms($post_id, $jobtime, true);
        wp_set_object_terms($post_id, $jobtypeger, true);

What do you think? I added it to my complete function:

//Create a dynamic post title, save relationship and taxonomy from parent to child by the CRED form.
add_action('cred_save_data', 'func_custom_post_title', 10, 2);
function func_custom_post_title($post_id, $form_data)
{
    if ($form_data['id'] == 28) {
        toolset_connect_posts('pracovni-nabidka-zadatel', $form_data['container_id'], $post_id);

        $joblocality = wp_get_object_terms($form_data['container_id'], 'lokalita');
        $jobtype = wp_get_object_terms($form_data['container_id'], 'obor');
        $jobtime = wp_get_object_terms($form_data['container_id'], 'uvazek');
        $jobtypeger = wp_get_object_terms($form_data['container_id'], 'remeslo');

        wp_set_object_terms($post_id, $joblocality, true);
        wp_set_object_terms($post_id, $jobtype, true);
        wp_set_object_terms($post_id, $jobtime, true);
        wp_set_object_terms($post_id, $jobtypeger, true);

        $name = get_post_meta($post_id, 'wpcf-name', true);
        $title = $name;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    } 
    else if ($form_data['id'] == 216) {
        $email = get_post_meta($post_id, 'wpcf-email-agent', true);
        $title = $email;
        $args = array('ID' => $post_id, 'post_title' => $title);
        wp_update_post($args);
    }
}

But it doesnt work 🙁

#1269403

I also tried:

 
        $joblocality = get_the_terms( $form_data['container_id'], 'lokalita' );
        wp_set_post_terms( $post_id, $joblocality, 'lokalita');

But it doesnt work too.

#1269441

Finally I used this:

add_action('cred_save_data', 'func_set_realtionship', 12, 2);
function func_set_realtionship($post_id, $form_data)
{
    if ($form_data['id'] == 28) {
        $term_list = wp_get_post_terms( $form_data['container_id'], 'lokalita', array( 'fields' => 'ids' ) );
        wp_set_post_terms( $post_id, $term_list, 'lokalita');
        $term_list = wp_get_post_terms( $form_data['container_id'], 'uvazek', array( 'fields' => 'ids' ) );
        wp_set_post_terms( $post_id, $term_list, 'uvazek');
        $term_list = wp_get_post_terms( $form_data['container_id'], 'obor', array( 'fields' => 'ids' ) );
        wp_set_post_terms( $post_id, $term_list, 'obor');
        $term_list = wp_get_post_terms( $form_data['container_id'], 'remeslo', array( 'fields' => 'ids' ) );
        wp_set_post_terms( $post_id, $term_list, 'remeslo');
    } 
}

And it somehow works. But is it right? And will it work with object cache right way?

#1270037

Hi, sorry I am not available on Fridays so I missed your messages.

And it somehow works. But is it right?
You should not rely on $form_data to set the parent post ID. It's better to add a hidden generic field in your Form and set the value of the field to be the current post's ID. Then use the value of the hidden field instead of $form_data['container_id'].

Add something like this hidden generic field in your form:

[cred_generic_field type='hidden' field='current-post-id']
{
"default":"[wpv-post-id id='$current_page']"
}
[/cred_generic_field]

Then in your callback, replace $form_data['container_id'] with the hidden field value:

$_POST['current-post-id']

Information about generic fields:
https://toolset.com/documentation/user-guides/inserting-generic-fields-into-forms/

And will it work with object cache right way?
Object cache shouldn't be a problem here, because we're simply getting and setting taxonomy terms using standard WP APIs. No complex queries at work here.

#1282923

My issue is resolved now. Thank you!