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
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?
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?
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?
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
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?
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
is the Job Posts ID
and argument
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?
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 🙁
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?
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.
My issue is resolved now. Thank you!