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

List of contractors with a custom search (wpml.org/contractors)
List of contractors with a custom search (see live demo

Individual contractor page with his portfolio
Individual contractor page with his portfolio (see live demo

Support for users in Toolset and WordPress

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:

 

Fig 1: Separating public user data in a custom post type
Fig 1: Separating public user data in a custom post type

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.

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.

Implementation

Structures

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.

Fig. 2: Post types and relationships with the user profiles set up by using the post author field
Fig. 2: Post types and relationships with the user profiles set up by using the post author field

 

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.

The flow

This is the flow you will need to handle:

  1. A user registers with your site.
  2. A WordPress profile (account) is created for him or her.
  3. The user logs in.
  4. He or she can create his or her Contractor profile now. He or she uses a front-end form.
  5. 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.
  6. 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.

Forms, Templates, and Views to create

To handle the flow, you will need to:

  1. Provide a way to create user profiles through the front-end (signup forms). You can create a User Form using Toolset.
  2. 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.
  3. 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).
  4. 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.
  5. Create a Content Template to display single Contractor pages.
  6. 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.

View with the post author filter
View with the post author filter

  1. Insert the View in the template you have created in the fifth step.

Contractor's showcases
Contractor’s showcases

  1. Create a Content Template to display single Showcase pages.
  2. 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.
  3. 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.

View with the post author filter
View with the post author filter

FAQ

How do I prevent users from creating more than one Contractor post?

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.

  1. Create a View of Contractor posts, filtered by post Author, where the current User is the author of the post.

Contractors created by the logged-in user
Contractors created by the logged-in user

  1. In the Loop Output editor of this View, skip the Loop Wizard and leave the wpv-loop tags empty.
  2. 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).
  3. Insert the View where you want your new Users to create their Contractor profile post.

View for Contractors that includes the form for creating new contractors
View for Contractors that includes the form for creating new contractors

Summary

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.