Hello, I'm building an association website that will have professional optometrist profiles and a user directory for the public ("Find an optometrist"). Link: hidden link
This tutorial seems to be the closest for what I'm trying to achieve: https://toolset.com/2022/12/how-to-let-wordpress-users-create-their-own-profiles-in-your-people-directory/#comments
I would however like to mix the views on the pubic directory to be a combination of profile fields (e.g. First Name and Last Name) and custom fields in a custom post type (e.g. Address), but it seems this is can't be done in way that allows the views filters to work properly. Is that correct?
The reason I don't want to (ideally) duplicate fields in the custom post type is to avoid data redundancy and duplication, and also to ensure that each optometrist is advertising themselves and not changing it to something else. So I guess if I can't use the "user meta" data directory, I could instead programmatically update those Profile fields on adding and editing the user meta?
I also want to make it so that only current paid-up members are listed in the public view. The users will all be a "member" role (managed by the Woocommerce memberships plugin), so can I just use a hook (using wpv_filter_query??) to just retrieve specific userIDs in that role to display? I guess I will have to duplicate the userID field in the "Profile" custom post type, so it knows which profiles to display?
I have in the past used a plugin like Profile Builder Pro, which just adds fields to the WordPress "user meta" and then has a simple filter and search (hidden link) ... and this keeps everything together in the user profile page. I appreciate that Toolset has the ability to add custom fields for Users (https://toolset.com/documentation/customizing-sites-using-php/creating-custom-user-profiles/), however apart from the search/filter issue above, for this current website, I also need to create complex relationships between individuals and companies (for billing purposes) so would it be best that I stick to Post Types in Toolset? One final question: can I do a front-end search on a field that is also used for relationships, like oganization?
Many thanks
Shaun
Hello,
Toolset Blocks plugin supports both post view and user view, post view supports custom search form, but user view does not support custom search form by default.
So I suggest you try these:
1) Setup a custom post type "optometrist"
2) Setup all those custom fields you want to put into the custom search form as post fields of post type "optometrist", for example:
- Address
3) Setup all those custom fields display only(don't need to put into search form) as user fields, for example:
- First Name
- Last Name
4) In the single "optometrist" post, you can display it's author information with shortcode: [wpv-post-author], like these:
[wpv-post-author format="meta" meta="last_name"]
[wpv-post-author format="meta" meta="first_name"]
https://toolset.com/documentation/programmer-reference/views/views-shortcodes/#wpv-post-author
For the question:
can I do a front-end search on a field that is also used for relationships, like organization?
Yes, the post view supports relationship filter, for example:
1) Create another post type "organization"
2) Setup one-to-many relationship between post types "organization" and "optometrist"
https://toolset.com/glossary/post-relationships/
3) Create a post view with custom search form, add a relationship filter into custom search form, see our document:
https://toolset.com/course-lesson/creating-a-custom-search/#how-to-search-by-post-relationships
OK thank you - that's helpful.
I know that relationships can't be made between Users and Custom Post Types, but is the following still possible?
Users can belong to only pre-approved organisations. So I have a Custom Post Type called "Organisations".
I create a "Select" Custom Field for Users (i.e. User Meta) called OrganisationID. I only allow Administrators/Editors to add organisations from the "Organisations" Custom Post Type (using the "wpt_field_options" filter) which populates the Select field for the Users.
Q1. How can I programmatically call the Organisations Toolset CPT to get the ID and Title values for the Select field?
Q2. In my custom Users View, how would I display the Organisation Name based on the CompanyID? Would I just create a shortcode to put inside the view, pass the ID into the shortcode which then outputs the companyname?
Can you show me a code example of how to programmatically do this?
Sorry for so many questions, I appreciate your help!
Thank you!
Q1) How can I programmatically call the Organisations Toolset CPT to get the ID and Title values for the Select field?
No, it does not custom codes, you can use custom reference field, which is based on one-to-many relationship
https://toolset.com/course-lesson/using-post-reference-field-to-set-up-one-to-many-relationships/
Q2) In my custom Users View, how would I display the Organisation Name based on the CompanyID? Would I just create a shortcode to put inside the view, pass the ID into the shortcode which then outputs the companyname?
You can connect user with custom post type, for example: setup that "Member" user as specific "optometrist" post's Author, each "Member" user can have only one "optometrist" post, then use this "optometrist" post connect other post types(organization). See our document:
https://toolset.com/course-lesson/how-to-create-custom-searches-and-relationships-for-users/
Thanks for your response.
Q1. The problem with the link you provided is that it doesn't allow relationships with Users. All I want is a dropdrown list on a front-end Edit Profile page, which is populated with the values from a custom post type. So I guess I can just do it with the wpt_field_options filter and then query the organisations CPT like so?
add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
$test = array();
if ($title == 'Select Organisation'){
global $post;
$args = array( 'post_type' => 'organisations', 'posts_per_page'=>-1, 'numberposts'=>-1);
$postslist = get_posts( $args );
foreach ($postslist as $key) {
$test[] = array(
'#value' => $key -> ID,
'#title' => $key -> post_title,
);
}
}
return $test;
}
I guess the other way to do this is by creating two CPTs and then with the front-end forms, then add and update the user profile fields with the "edit_user_profile" action, but because "organisation" is the only custom field I need, and the rest are User meta fields, it's much easier for me to update the native user object with a User Custom Field called "OrganisationID" and then add the above code as a custom field to populate it.
It is not Toolset built-in feature, and you are right, it is possible with custom codes.
I have tried it in a fresh WP installation, see below test site:
Login URL:
hidden link
1) Create a custom post type "Organisations" using slug "organisations":
hidden link
2) Add one user field "Select Organisation":
hidden link
3) Add a custom code snippet:
hidden link
with below codes:
add_filter( 'wpt_field_options', 'populate_select', 10, 3);
function populate_select( $options, $title, $type ){
if ($title == 'Select Organisation'){
$options = array();
$options[] = array(
'#value' => '',
'#title' => 'Choose one',
);
$args = array( 'post_type' => 'organisations', 'posts_per_page'=>-1, 'numberposts'=>-1);
$postslist = get_posts( $args );
foreach ($postslist as $key) {
$options[] = array(
'#value' => $key -> ID,
'#title' => $key -> post_title,
);
}
}
return $options;
}
4) Test it in backend:
hidden link
And frontend user form:
hidden link
Both works fine, for your reference.
More help:
https://toolset.com/documentation/programmer-reference/types-api-filters/#wpt_field_options
Thank you for proving that works. Much appreciated!