WordPress can display “posts” in many flexible ways. However, it is very limited in the display of “users.” In this tutorial, we explain how to create custom searches and relationships for users by using posts.
Suppose you build a site where users create their profiles through the front-end and other visitors can browse those profiles and search for the specialist they need.
In some cases, these users might also need to submit their additional work, each piece of their work being a separate entry. Some of the examples would include:
- Artists who submit their paintings
- Music composers who submit their songs
- Contractors to be hired who submit the websites they have built
Using Toolset, you cannot create a custom search for user profiles.
WordPress itself does not provide any template for displaying individual user profiles on the front-end. That is because WordPress is designed more to display users as post authors.
However if you mirror user information into a custom post type, you can achieve what you need.
We will still use WordPress user profiles but only to store basic user information that is essential for maintaining user accounts (username, password, etc.). All remaining information will be stored in a dedicated post type—in our case, the Contractor.
The image below illustrates this idea:
So, how do you create a connection between user profiles and your post type?
In WordPress, when a user creates a new post, his or her username is set in the post author field. This is when the “connection” between the new post and the user profile is made.
By using Toolset Forms, users can create posts by themselves through the front-end. We only need to ensure that the user submits the form when he or she is logged-in. In this way, the relevant username will be saved with each new post in the post author.
Here are the structures you will need in order to implement the case with contractors and their showcases:
1. WordPress User Profiles
Use WordPress user profiles to store the username and basic information about the user, such as his or her e-mail and first and last names.
2. Contractors – Custom Post Type for storing additional user information
Create a custom post type and fields representing Contractors. Each user will have only one post based on this custom post type. Contractor posts will be available to the public.
3. Showcase – Custom Post Type for storing multiple instances of the user’s work
If you need to store more than one item representing the user’s work/portfolio, like Websites or Mobile Apps, create a separate post type for it (in our case it will be Showcase). This will allow you to list all the items in a custom way and even create a separate search for the items.
Add custom fields to your Showcase post type.
Alternatively, you could create a regular one-to-many relationship between Contractors and Showcases, but since the showcase entries will be added by the logged-in user through the front-end forms, it will be easier to use the same approach for both Contractors and Showcases.
This is the flow you will need to handle:
- A user registers with your site.
- A WordPress profile (account) is created for him or her.
- The user logs in.
- He or she can create his or her Contractor profile now. He or she uses a front-end form.
- The user waits for the site administrator to approve the contractor post. The Contractor post becomes public. It is available on the list of all Contractors so that other visitors can search for it.
- The contractor can submit his or her work/portfolio by using a front-end form. The showcase entries become available to the public on the contractor’s individual page and on the list of all showcase entries.
To handle the flow, you will need to:
- Provide a way to create user profiles through the front-end (signup forms). You can create a User Form using Toolset.
- Provide a way to allow your users to log in to the site (login forms). You can use the Toolset Views plugin and the Login Form shortcode.
- Create a Post Form to add new Contractors (use the Toolset Forms plugin). Make this post form available only to logged-in users (use the Toolset Access plugin).
- Create a Post Form to add new Showcase entries. Again, use Toolset Access to make sure that this post form is available only to logged-in users.
- Create a Content Template to display single Contractor pages.
- Create a View that displays all the showcases that belong to a given Contractor. Use the Post author filter: Select post with the author the same as the page where the View is shown.
- Insert the View in the template you have created in the fifth step.
- Create a Content Template to display single Showcase pages.
- Create a View with a search to display all Contractors and allow visitors to filter the Contractors by using the selection criteria. Alternatively, you can create an archive for Contractor posts.
- If you want your users to see all the showcase posts they created, create a View and use the Post author filter: Select posts with the author the same as the current logged in user filter.
Here’s a trick you can do to hide your new Contractor Form from (created in #3) from any user who has already created their related contractor post.
- Create a View of Contractor posts, filtered by post Author, where the current User is the author of the post.
- In the Loop Output editor of this View, skip the Loop Wizard and leave the wpv-loop tags empty.
- In the wpv-no-items-found tags, insert the Form to create a new Contractor (the one you create in #3 in the previous section).
- Insert the View where you want your new Users to create their Contractor profile post.
In Toolset, you can create relationships only between your post types, and these relationships should cover most typical cases.
However, when you need to extend your user information with additional data that can be searchable by other users, it makes a lot of sense to connect user profiles to post types using the post author field.