Skip Navigation

[Resolved] Category auto with cred form

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

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 3 replies, has 3 voices.

Last updated by Beda 4 years, 12 months ago.

Assisted by: Beda.

Author
Posts
#1389611

Hello,

I tried to add the following PHP code in order to directly assign a category when a new post is created through a CRED Form :
I followed the instructions of : https://toolset.com/forums/topic/want-cred-form-to-post-to-default-category/

I added the code for 2 forms
CRED FORM ID : 86122
Custom taxonomy ID: 1659
and
CRED FORM ID : 87980
Custom taxonomy ID: 1764

But I have a Php error and it crash the website.
I probably did something wrong.

Could you help please ?


/**
 * Automatic category Label N1 for NEW CAS
 */
add_action('cred_save_data', 'set_category',10,2);
function set_category($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==86122)
  
{
 
 $my_post = array(
      'ID'           => $post_id,
      'post_category' => array(1659) // id's of categories
  );
 wp_update_post( $my_post );
  
}
  
}

/**
 * Automatic category Label N4 for Chanteur Connu
 */
add_action('cred_save_data', 'set_category',10,2);
function set_category($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==87980)
  
{
 
 $my_post = array(
      'ID'           => $post_id,
      'post_category' => array(1764) // id's of categories
  );
 wp_update_post( $my_post );
  
}
  
}
#1390015

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Serge,

Thank you for getting in touch.

To correctly set the default taxonomy term you will need to use the function below.
https://developer.wordpress.org/reference/functions/wp_set_post_terms/

Please try this and let me know if it helps.

Thanks,
Shane

#1390273

Hello Shane,

Thanks a lot.
Unfortunately, I am not a developper myself and i might need a little bit of help on how to implement the code if that's alright.

I basically need to add a custom taxonomy (id: 1659) to the CPT1 when a CPT1 is created through the form with ID : 86122.

The category is not hierachical.

Thank you in advance for your help.

#1392161

When you are not familiar with PHP and WordPress programming, the right thing to do would be to either consult forums where PHP help is given, or the WordPress forum, or our consultants https://toolset.com/contractors/.

This is because Toolset Support can only give limited assistance (in the margin as Shane already provided it).
https://toolset.com/toolset-support-policy/

WordPress supports adding categories, which are the native taxonomies used on Posts, with wp_update_post, as you can see here in the source code:
https://developer.wordpress.org/reference/functions/wp_update_post/

But you need to add a custom taxonomy, and likely to a custom post in a Toolset Form, so you need to use what Shane suggested - https://developer.wordpress.org/reference/functions/wp_set_post_terms/

While you can consult on the WordPress developer DOC how to use that method, you can see below an example and the DOC about the Forms API to use.
I would use cred_save_data() here and hook wp_set_post_terms in it.
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

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']==12)//Change this ID to the Form ID you use
    {
        //All this inside here is custom code
        //Here you could update a post, or do whatever other custom action you want. For example, wp_set_post_terms()
       //https://developer.wordpress.org/reference/functions/wp_set_post_terms/, which is clarified about how to use it in the doc.
       //note that the cred_save_data API already provides you with the Post ID so you do not need to find that - you can use $post_id
       //Then the WP Codex says that there should be a string or array of $tags, meaning an array of terms to set for the post, or a string of terms separated by commas, which should be Term IDs usually. For example like below. Then it also goes on explaining that you should pass the actual taxonomy they should belong to as a string, which I already as well did below in the example. Ultimately you finish the method with the $append argument, which can be true or false (I left it empty, so it is set by default to false), which will decide whether add new terms to the post keeping existing or deleting existing.
       wp_set_post_terms( $post_id, '1, 2', 'your_taxonomy' )
    }
}