Home › Toolset Professional Support › [Resolved] assign parent category to child post
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.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
Tagged: CRED API, Toolset Forms
Related documentation:
This topic contains 13 replies, has 2 voices.
Last updated by eliseD-2 7 years, 7 months ago.
Assisted by: Minesh.
I would like to automatically assign a category to a post based on the category of the parent.
I am using a cred child post link - so the parent is already being set. How can I make sure the new child post is automatically assigned whatever category is assigned to the parent?
Thanks!
Elise
Hello. Thank you for contacting the Toolset support.
You can use CRED hook "cred_save_data" with wp_get_object_terms() to get the post terms and wp_set_object_terms() to set the post terms.
I can see you have created another ticket:
https://toolset.com/forums/topic/cred-saved-data-hook-not-working/
We will continue with the above ticket as this is duplicate ticket.
The other ticket is different because I am using that cred save data hook in a series of if loops for a finite number of options.
For this one - I need to assign the category based on whatever is the category of the parent of the post being created (so that is going to change for every post and I don't want an if loop for each one)
So - can you help me figure out exactly how to use the cred save data hook and the wp_get_object_terms() to accomplish this goal?
Thanks!
Elise
Yes.
1)
Could you please share problem URL of CRED form?
2)
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).
I have set the next reply to private which means only you and I have access to it.
You should try following code with the CRED hook: cred_save_data
For example:
add_action('cred_save_data', 'my_save_data_action',10,2); function my_save_data_action($post_id, $form_data) { // if a specific form if ($form_data['id']==699) { $parent_terms = get_the_terms( $parent_post_id, 'your-category-slug' ); if($parent_terms ) { $parent_term_ids = array(); foreach($parent_terms as $parent_term) { if($parent_term->term_id != '') { $parent_term_ids[] = $track_term->term_id; } } if(count($parent_term_ids) > 0) { wp_set_post_terms( $post_id, $parent_term_ids , 'your-category-slug' ); } } }
Where:
ger your $parent_post_id to fetch perant post terms and adjust your taxonomy slugs with 'your-category-slug'
More info:
https://toolset.com/documentation/user-guides/cred-api/#csd
Thanks!
Just to confirm - "your-category-slug" would be the slug for my custom taxonomy name? Which is lab-cateogries - as opposed to a specific category slug?
I have the custom category type "lab-categories" - and under that taxonomy name, there are these categories:
Book Lab
Music Lab
Lyric Lab
etc.
I'm hoping I don't have to create a separate cred save hook for every single specific category (and then remember to add another cred hook every time I create a new category).
So I just wanted to double-check - do I just put the taxonomy name in there?
Thanks for the clarification.
Elise
Yes - you need to replace it with your taxonomy slug (lab-categories) and try to resolve your issue.
For example:
$parent_terms = get_the_terms( $parent_post_id, 'lab-categories' );
Thanks. I tried this out - but it didn't seem to work for me.
First time I put it in- it crashed my site. I think it was missing a bracket. I added one more bracket at the very end - which solved the crashing site issue - but the child post that was created did NOT have a category assigned to it.
Here is what I entered in my functions.php: (I have several other save-date hooks already - so I couldn't use the generic name)
add_action('cred_save_data', 'category_save_data_action',20,2);
function category_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==699) {
$parent_terms = get_the_terms( $parent_post_id, 'lab-categories' );
if($parent_terms ) {
$parent_term_ids = array();
foreach($parent_terms as $parent_term) {
if($parent_term->term_id != '') {
$parent_term_ids[] = $track_term->term_id;
}
}
if(count($parent_term_ids) > 0) {
wp_set_post_terms( $post_id, $parent_term_ids , 'lab-categories' );
}
}
}
}
I think you are missing something here. Could you please confirm that you have followed the correct way to configure your CRED child forms:
=> https://toolset.com/documentation/user-guides/cred-forms-for-child-content/
Also, I do not get CRED parent post id. Could you please tell me what is the parent/child relationship between forms?
I need step by step information in order to help you further and to fix your issue.
The parent CPT is Assignments
The child CPT is AssignmentResults
The taxonomy (assigned to both CPTs) is Lab Categories (lab-categories)
I have created a CRED form to add a new AssignmentResults post:
Form Name "AssignmentResult Add Form" Form ID 699:
[credform class='cred-form cred-keep-original']
[cred_field field='form_messages' value='']
[cred_post_parent get='ID']
[cred_generic_field field='post_title' type='textfield' class='' urlparam='']
{
"required":1,
"validate_format":0,
"default":"[cred_post_parent]"
}
[/cred_generic_field]
<br><br>When you are ready to take this assignment, press the SUBMIT button below. You will be taken back to the Lab Home Page - and you will need to click the PENDING link for the appropriate assignment.
<br><br>[cred_field field='form_submit' value='Submit' urlparam='']
[/credform]
This form gets the title of the parent post from Assignments and makes it ALSO the title of the child post.
This form is inserted on a page called "Assignment Set Up" (ID #701) with this code:
[cred_form form='699' form_name='AssignmentResult Add Form']
This all already works exactly the way I want to create a new child post with the same title as the parent.
What I want it to ALSO do is to automatically assign that new child post to the same lab-category as the parent post (without the front end user having to see a list of possible lab categories).
Let me know if you need any more information.
Thanks.
Elise
The thing is that I do not see proper parent assignment title. Please check following image:
=> hidden link
For me its really important to know how can I see first Assignment(parent) title in the child form title textbox. Do I need to setup any data or could you please share right login.
I do not see any Assignment with title "Previous Page' thats the issue I have. Once I can see right assignment title on child CRED form its good to go to take next steps for me.
Ah, yes, sorry. You need to reach that page via the page with the child-cred-post link on it; and you have to have been signed in as a user to see that page properly.
Here's what you can do:
login to my site as my test user:
username: labtester
password: wmt
You will be taken to lab-home-page - which will populate with several charts of information about the labs.
You need to scroll to the 2nd chart that is titled "Assignment Results Summary". Then click the "Submit" link for any of the assignments in the chart.
That "Submit" link is a cred-child-post link that will take you to the Assignments Set Up page with the correct info about the parent.
Thanks.
Elise
Thank you for the information.
I've made some amendments to the code you added to functions.php :
add_action('cred_save_data', 'category_save_data_action',20,2); function category_save_data_action($post_id, $form_data){ // if a specific form if ($form_data['id']==699) { $parent_post_id = $_GET['parent_assignment_id']; $parent_terms = wp_get_object_terms( $parent_post_id, 'lab-categories', array('ids') ); if(!empty($parent_terms)) { $parent_term_ids = array(); foreach($parent_terms as $parent_term) { if($parent_term->term_id != '') { $parent_term_ids[] = $parent_term->term_id; } } if(count($parent_term_ids) > 0) { wp_set_object_terms( $post_id, $parent_term_ids , 'lab-categories' ); } } } }
I can see that now, when you save child form it will automatically assign parent post taxonomy terms to child post.
I hope this solution will help you to resolve your issue.
Yay! Yes - that worked!
Thank you SO much!
Elise