Skip Navigation

[Resolved] Set default category for posts

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

Problem:
How to set default taxonomy term when you add create a entry using CRED form.

Solution:
You can use the "cred_save_data" hook to make any amendments. you can add "cred_save_data" hook to your functions.php file.

Please find following link with solution:
https://toolset.com/forums/topic/set-default-category-for-posts/#post-389970

Relevant Documentation:
https://toolset.com/documentation/user-guides/cred-api/#csd

This support ticket is created 8 years 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.

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)

This topic contains 9 replies, has 2 voices.

Last updated by carstenM 8 years ago.

Assisted by: Minesh.

Author
Posts
#389746

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']

#389841

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#389863

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.

[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='']

<div class="cred-field cred-field-post_title">
<label class="cred-label">
Title
</label>
[cred_field field='post_title' post='post' value='' urlparam='']
</div>

<div class="cred-field cred-field-post_content">
<label class="cred-label">
Post
</label>
[cred_field field='post_content' post='post' value='' urlparam='']
</div>

<div class="cred-field cred-field-_featured_image">
<label class="cred-label">
Featured Image
</label>
[cred_field field='_featured_image' value='' urlparam='']
</div>

<div class="cred-group cred-group-taxonomies">
<div class="cred-taxonomy cred-taxonomy-category">
<div class="cred-header"><h3>Category</h3></div>
[cred_field field='category' display='checkbox']
</div>
</div>

[cred_field field='form_submit' value='Submit' urlparam='']

[/credform]

#389868

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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 );
}
}

Where:
Replace '111' with your CRED form ID

More info:
https://toolset.com/documentation/user-guides/cred-api/

Few related tickets:

#389875

As I understand it. If I reply this to functions.php this will only affect affect this CRED form and not all my other forms?

#389878

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - as you can see we have if condition:

if ($form_data["id"]==111)
{
 

So here we are checking form ID. So you should replace '111' with your original CRED form ID and it will affect to your xxx form ID only.

#389894
category.jpg
functions.jpg

Hi

My CRED forms now looks like this. When I make a new post with the forms the taxonomy category get the name from category id=1 called "Vigtig nyhed"

I have submitted 2 images of my category and functions.php code.

APLYED TOFUNCTIONS.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"]==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
---------------------------------------------------------

[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='']

<div class="cred-field cred-field-post_title">
<label class="cred-label">
<h4><b>Nyhedsoverskrift</b></h4>
</label>
[cred_field field='post_title' post='post' value='' urlparam='']
</div>
<br><br>
<div class="cred-field cred-field-post_content">
<label class="cred-label">
<h4><b>Nyhed</b></h4>
</label>
[cred_field field='post_content' post='post' value='' urlparam='']
</div>
<br><br>
<div class="cred-field cred-field-_featured_image">
<label class="cred-label">
<h4><b>Nyhedsbillede</b></h4>
</label>
[cred_field field='_featured_image' value='' urlparam='']
</div>
<br><br>

[cred_field field='form_submit' value='Udgiv' urlparam='']

[/credform]

#389926

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I think there is a little typo error.

Could you please try following code:

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 );
}
}
#389965

Sorry - It dosen't work

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 );
}
}

My CRED form looks like this:

[credform class='cred-form cred-keep-original']

[cred_field field='form_messages' value='']

<div class="cred-field cred-field-post_title">
<label class="cred-label">
<h4><b>Nyhedsoverskrift</b></h4>
</label>
[cred_field field='post_title' post='post' value='' urlparam='']
</div>
<br><br>
<div class="cred-field cred-field-post_content">
<label class="cred-label">
<h4><b>Nyhed</b></h4>
</label>
[cred_field field='post_content' post='post' value='' urlparam='']
</div>
<br><br>
<div class="cred-field cred-field-_featured_image">
<label class="cred-label">
<h4><b>Nyhedsbillede</b></h4>
</label>
[cred_field field='_featured_image' value='' urlparam='']
</div>
<br><br>

[cred_field field='form_submit' value='Udgiv' urlparam='']

[/credform]

#389970

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.