I made a user field where someone can enter the name of their company in their user profile. This will create a post category with the same name.
I then created a post submission form (using CRED) for frontend. So my question is, what would the function be so the post the user submits in the frontend always is posted only to the category that is listed in their profile they made?
Example: If Mike has company name of 'Lakewood' then when Mike creates post using cred form on frontend, post will automatically be assigned to the post category 'Lakewood'.
I see creating the Term with the same name as the Field's value as entered by the User is currently under progress here:
https://toolset.com/forums/topic/create-category-per-user/#post-1233377
I see now that you want to add the term corresponding to each user automatically when submitting new posts.
I think the cleanest here is a Custom PHP snippet as well.
You can hook it also to cred_save_data(), just here we will get the current users metadata and update the posts meta with this, on form submission.
You'll again use Forms API; WordPress API and PHP:
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_post_categories
https://codex.wordpress.org/Function_Reference/get_term_by
A code snippet to be adapted can look like this:
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)
{
//Get current user
$current_user = wp_get_current_user();
//Get current users meta
$user_meta = get_user_meta($current_user->ID , 'wpcf-user_meta_field', true);
//get term ID
$term_object = get_term_by( $slug, $user_meta, 'category' ) ;
//update post term with user meta
wp_set_post_categories( $post_id, $term_object->term_id, true);
}
}
Please check the DOC and functions used carefully to adapt them to your use case.
So I tried this
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']==3101)
{
//Get current user
$current_user = wp_get_current_user();
//Get current users meta
$user_meta = get_user_meta($current_user->ID , 'wpcf-church', true);
//get term ID
$term_object = get_term_by( $slug, $user_meta, 'category' ) ;
//update post term with user meta
wp_set_post_categories( $post_id, $term_object->term_id, true);
}
}
I put 'laurelwood' as my church name in my user profile (wpcf-church). I then submitted the form (3101) in frontend and it did not go into the category named 'laurelwood'. Is there another data in the code above I'm supposed to change?
Well, since https://toolset.com/forums/topic/create-category-per-user/#post-1233377 is failing with errors I do not expect the code above which depends on the working solution of https://toolset.com/forums/topic/create-category-per-user/#post-1233377 to work either.
Now, my code shared is a sample here as well, so if your taxonomy is NOT categories, you'd have to change that.
If it is native Categories, you can use the code but need to adapt it.
You will need to consult the DOC I linked and adapt the code to your site.
For example, get_term_by() needs to be changed, please consult the DOC.
The first parameter needs to be a string, such as 'id', 'slug', 'name', or 'term_taxonomy_id' - it depends what you want to search for, and in your case, it's as far I understand, 'slug'.
So the function would be get_term_by( 'slug', $user_meta, 'category' ) ;, this, only if you look in the native Taxonomy "Category" for a term slug named as stored in $user_meta.
As mentioned in the other ticket, Toolset Support cannot produce tailored custom code ready for the particular site, but can give examples which with the study of the Codex we link, will help to create code that we can review if necessary but not craft "ready to go".
Thanks!