I am trying to: Create a CRED form where the field post category (taxonomies) is set to default "report". The form has fields for Title, Post and Category (taxonomies).
Normally my post has 5 category to choose from. But I want a CRED form where the category "report" has been choose as default, and can't be change.
When use this line, I got 5 checkbox to thoose the taxonomies category from.
[cred_field field='category' display='checkbox']
Hello. Thank you for contacting the Toolset support.
As I understand - you want to display all taxonomy terms using checkbox and only one term "report" should be remain checked and user should not able to modify it.
If this is correct - Could you please share problem URL where you added CRED form?
In the best world I only want the users to fill the field post title and post. The field category should not be shown in the form and set to "Report" as default when a post is created.
My Cred forms looks like this with the option to choose taxonomy/category. I want to remove the option to choose taxonomy/category but set it to "Report" in the background.
As you don't want to display the category on your CRED form. you can use the "cred_save_data" hook to set the taxonomy for the post. you can add "cred_save_data" hook to your functions.php file.
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"]==111)
{
if(!has_term('','category',$post_ID)){
$category_ids = array(6); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');
}
wp_update_post( $my_post );
}
}
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"]==6177)
{
if(!has_term('','category',$post_ID)){
$category_ids = array(202); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');
}
wp_update_post( $my_post );
}
}
CODE IN MY CRED FORM WITH ID 6177
---------------------------------------------------------
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"]==111)
{
if(!has_term('','category',$post_id)){
$category_ids = array(6); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');
}
wp_update_post( $my_post );
}
}
When I make a new post with the form category get id 1 with the name "Vigtig nyhed" and not the id 202 called "Report"
The category has an ID 202 and the CRED form ID is 6177
I hav put this to functions.php:
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"]==111)
{
if(!has_term('','category',$post_id)){
$category_ids = array(6); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');
}
wp_update_post( $my_post );
}
}
I think you forget to replace '111' with your form ID.
Could you please try following exact code where I've replace your CRED form ID as well as category ID.
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"]==6177)
{
$category_ids = array(202); // replace your category ID
wp_set_object_terms( $post_id, $category_ids, 'category');
}
}
I hope above solution will help you to resolve your issue. If above solution does not help:
Please share problem URL:
*** 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.
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.