Tell us what you are trying to do?
I already set up a view/form where the a specific user can update their profile using a user form. I need them to be able to select an specific post from a Custom Post Type. This should be a select dropdown where they select the post. In the image, Building is the custom post the user should be able to select. This reference should be displayed in WordPress REST Api.
There is no problem if I need to code a solution, the problem is that I don't know where to start.
Is there any documentation that you are following?
No. I notice that using Custom Post Fields inside User Fields is not possible but I need a way to create the relationship.
Hello, Toolset's post relationships feature works between post types, so we typically recommend creating a "proxy" post type for Users to enable post relationships and custom search for User profiles. We have some documentation available for this process here: https://toolset.com/course-lesson/how-to-create-custom-searches-and-relationships-for-users/
However, this process would not enable you to choose a related post in an Edit User Form.
Another approach is to save a simple number as a custom field in the User Profile. That number would be the post ID of the post you want to relate to their profile. So the solution depends on the approach you want to take. One option is to create a custom field in the User Profile that stores a simple number, which would be the ID of the post they choose. You could use that information on the front-end of the site to display the related post information. This would not be a true post relationship, just a reference by ID. I could show you how to set up a generic select field in the User Form to include an option for each post, and let the User select the post they want to relate to their own profile. In the User profile of wp-admin, this field would show up as a simple number.
The other option is a true post relationship with a proxy post type for Users, and it's a bit more complicated because usually you would need to manage that post relationship in a separate Form from the Edit User Form.
Let me know which approach you prefer and we can discuss implementation, or if you have questions let me know.
Hi Christian, thank you for your answer.
I think for my specific case, creating only a reference will be the best since I can use it later to look into the specific custom type.
So far I created a field in the admin dashboard to select the specific building (image attached), save it in the DB and show it in the meta data of user when displaying users when using the REST API.
Now, I don't know how to actually enable that field I created in the admin dashboard into the Toolset User From. What I'm missing or what should I do?
If there is any specific approach I should do please advice. I'm open to apply another thing as long as I can capture the user input with a dropdown, just referencing the post and displaying it into the users REST API.
Now, I don't know how to actually enable that field I created in the admin dashboard into the Toolset User From. What I'm missing or what should I do?
Forms is integrated with Toolset Types custom fields, so if you created this custom field using Toolset Types, you will find the field in the User Form builder automatically. You can drag the field into the drag-and-drop Form builder to display the field in a User Form.
If you created a custom field using custom code, there is no simple integration between Toolset's User Forms and those custom fields created in code. You would need to insert the field in the Form using custom code, like a custom shortcode that outputs the field. You can use an HTML Content block to insert your own custom shortcode in a Form, or use the Expert builder to work with a full shortcode interface. Then you would probably need to use the Forms API to save the value of that field in the User's profile using update_usermeta. Example:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($user_id, $form_data)
{
// if a specific form
if ($form_data['id']==12)
{
if (isset($_POST['my_custom_field']))
{
// add it to saved post meta
update_usermeta($user_id, '__my_custom_field', $_POST['my_custom_field'], true);
}
}
}
We have documentation for the Forms API available here: https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
I notice that Toolset an option in some generic fields to input a shortcode to display fields. I tried to use the generic select field but I haven't been able to display the select field. This is the JSON output I'm sending. Did I miss something?
I notice that I forgot to put the slug into the Generic form field and after removing the first [] from the JSON I manage to displayed the fields. Also I used the code you suggested and it work, however there was a typo in update usermeta, should be update_usermeta not update_user_meta.
Thank you so much for your help my issue is resolved!
My issue is resolved now. Thank you!
Thank you for reporting the typo in my code - I have updated the example for future reference.