Skip Navigation

How to Create Custom Searches and Relationships for Users in WordPress

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
List of contractors with a custom search (see live demo)
Individual contractor page with his portfolio (see live demo)

Support for users in Toolset and WordPress

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:

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, 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.

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 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.

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. User can use a front-end form to create Contractor profile now.
  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 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.

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 Insert the registration form into a page.
  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 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.
View with the post author filter
  1. 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.
  2. 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.
View with the post author filter

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 page where the newly registered users can create their Contractor profile post.
  2. 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.
Contractors created by the logged-in user
  1. 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:
Insert contractor form
[cred_form form="your-form-slug"]
Use shortcode to insert form

Replace 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.

Form slug

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.

Updated
June 8, 2021