WordPress can display “posts” in many flexible ways. However, it is very limited in the display of “users.” In this lesson, 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 can’t 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, a 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 users submit the form when they are logged-in. In this way, the relevant username will be saved with each new post in the post author.
Please note that we do not use the regular Toolset Relationship feature in this case. Instead of setting up a relationship between two post types, we are using the post author field to “connect” WordPress profiles to a post type.
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 e-mail, 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.
- User can use a front-end form to create Contractor profile now.
- 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 use a front-end form to submit work/portfolio. 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 Insert the registration form into a page.
- 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 and inside, create a View that displays Showcases. Select the View block, in the right sidebar expand the Content Selection section, and click Add a filter. Select the Post author filter and then the Post author is the author of the page where this View is shown option.
- Create a Content Template to display single Showcase pages and inside, create a View that displays Contractors. Add custom search to the View to 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 themselves, create a View that displays Showcases. Select the View block, in the right sidebar expand the Content Selection section, and click Add a filter. Select the Post author filter and then the Post author is the same as the logged in user option.
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 page where the newly registered users can create their Contractor profile post.
- Add a View that lists Contractor posts. Select the View block, in the right sidebar expand the Content Selection section, and click Add a filter. Select the Post author filter and then the Post author is the same as the logged in user option.
- Don’t add any blocks to the View Loop. Instead, select the View Loop block, in the right sidebar expand the Loop Template section, and add the following shortcode:
your-form-slug with the slug of your form for creating contractor post type. You can find the form’s slug by editing it and looking at the top of the page.
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.
- Styling Custom Searches
- Displaying Search Results on a Different Page
- Searching Texts in Custom Fields with Toolset and Relevanssi
- Using Post Reference Field to Set-Up One-to-Many Relationships
- Selecting Parent Posts when Using Forms to Create Child Items
- How to set up Post Relationships in WordPress
- Front-End Forms for Connecting Related Posts
- Displaying Related Posts
- What Are Post Relationships and How They Work
- Many-to-Many Post Relationships in WordPress
- Creating a Custom Search
- Creating Responsive Columns and Grids
- Display Content Conditionally