Skip Navigation

[Resolved] Assign category to new post, based on author user fields

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

Problem: I have a Form that allows Users to submit posts. I would like to automatically assign different taxonomy terms to each post based on custom fields in the author's User profile.

Solution: Use the cred_save_data hook to add terms to a post created by Forms. Create a custom field on the User profile that can be used in the cred_save_data callback to determine which terms should be added to the post.

add_action('cred_save_data', 'set_blog_terms',100,2);
   
function set_blog_terms($post_id, $form_data) {
  global $current_user;
  $forms = array( 9544 );
    if (in_array($form_data['id'], $forms))
    {
      $array = array(
        'malawi-2018' => array('blogi','kapua-2018-malawi'),
        'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
      );
      $taxonomy = 'category';
      $userFieldValue = get_user_meta( $current_user->ID, 'wpcf-kapuaja-hanke', true);
      if(!$userFieldValue) { 
        return;
      }
      $tag = $array[$userFieldValue];
      wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://codex.wordpress.org/Function_Reference/get_user_meta
https://codex.wordpress.org/Function_Reference/wp_set_object_terms

This support ticket is created 6 years, 3 months 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.

Our next available supporter will start replying to tickets in about 0.54 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 7 replies, has 2 voices.

Last updated by olliM-3 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1071395

Hi,

I want users on my site to be able to write blog posts but I want the new posts to be assigned to a specific category based on a user field.

For example the user has a User Field called Team and it is selected as Sweden 2018.
When that user writes a blog post using a Cred Post Submission form I want that post to be assigned to the category Blog and subcategory Team Sweden 2018

Right now the problem is that users can see and select all the categories on the Cred form and they are choosing the wrong categories and I have to go around fixing their blog categories.

#1071523

Hi, you can use the cred_save_data hook to programmatically set specific terms on a post created by Forms. First, edit the Form and remove the taxonomy input fields so the Users can't make any term selections. Then use this code template to specify which term(s) correspond to each possible User field value and apply those terms to the new post:

add_action('cred_save_data', 'set_blog_terms',100,2);

function set_blog_terms($post_id, $form_data) {
  global $current_user;
  $forms = array( 999 );
    if (in_array($form_data['id'], $forms))
    {
      $array = [
        '123' => array('blog','team-sweden-2018'),
        '456' => array('blog', 'team-norway-2017'),
      ];
      $taxonomy = 'category';
      $userFieldValue = get_user_meta( $current_user->ID, 'wpcf-userfieldslug', true);
      $tag = array( $array[$userFieldValue] );
      wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

Replace 999 with the numeric ID of this Form, or a comma-separated list of Form IDs you want to include in this filter. Then in the $array section, you will create one line for each possible value in the User field. For example, let's say it's a select field, and one option is "Team Sweden" with the content or value team-sweden. Replace 123 with team-sweden, and replace 'blog', 'team-sweden-2018' with a comma-separated list of term slugs you would like to assign to the new post. Surround each term slug in single quotes. Replace userfieldslug with the slug of your User field.

This may need to be adjusted slightly depending on your setup, so I'll need more information if you need additional assistance.

#1075050
toolset_support_kap.png

Hi,

Thanks for the code.

I tested the blog writing form with a test user. I was able to write a blog post but the wrong category was selected for the post.

The user field is a Radio select and it only has two possible selections at the moment (malawi-2018 and oma-kapua-nepal-2018).

This is what I added this to my child themes functions.php.

add_action('cred_save_data', 'set_blog_terms',100,2);
 
function set_blog_terms($post_id, $form_data) {
  global $current_user;
  $forms = array( 9544 );
    if (in_array($form_data['id'], $forms))
    {
		$array = [
		'malawi-2018' => array('blogi','kapua-2018-malawi'),
		'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
      ];
      $taxonomy = 'category';
      $userFieldValue = get_user_meta( $current_user->ID, 'wpcf-kapuaja-hanke', true);
      $tag = array( $array[$userFieldValue] );
      wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

I attached a screenshot of the user field settings.
Is the user fields Radio select field the culprit?

#1076350

This code looks okay to me, and the radio field seems to be set up correctly. May I log in and see how this is set up in wp-admin? Also please tell me which User account you used to test the Form, and where I can find the Form on the front-end of the site.

#1078606

Okay here's an update that should resolve the syntax problems:

add_action('cred_save_data', 'set_blog_terms',100,2);
  
function set_blog_terms($post_id, $form_data) {
  global $current_user;
  $forms = array( 9544 );
    if (in_array($form_data['id'], $forms))
    {
      $array = [
        'malawi-2018' => array('blogi','kapua-2018-malawi'),
        'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
      ];
      $taxonomy = 'category';
      $userFieldValue = get_user_meta( $current_user->ID, 'wpcf-kapuaja-hanke', true);
      if(!$userFieldValue) { 
        return;
      }
      $tag = $array[$userFieldValue];
      wp_set_object_terms( $post_id, $tag, $taxonomy, true );
  }
}

Dreamweaver also gave me an error and said the code had syntax errors on these lines:
In the code sample you provided, it looks like the forum changed some of the brackets to HTML entities somehow, they show up as the entities 091 and 093. Check the code I provided above and compare it to the code in your sample. The syntax works fine for me, can you tell me what version of PHP you are running?

#1078785

Hi,

Thanks for the quick reply.
I'm running PHP version 5.6.27
I will try the code you posted tomorrow and give you an update.

#1078791

Sure, if it continues to complain you can try this variation:

$array = array(
        'malawi-2018' => array('blogi','kapua-2018-malawi'),
        'oma-kapua-nepal-2018' => array('blogi','oma-kapua-2018-nepal'),
      );
#1084544

Hi Christian,

Sorry for the delay.

I inserted the code to my child theme's functions.php and it works beautifully now.
When users with a certain user field write blog posts using the Cred form their posts get the correct category assigned to them automatically.
This is a really valuable feature and saves me a lot of trouble and makes it easy for the users to write blog posts.

Thank you!