{"id":1929573,"date":"2021-02-17T12:04:09","date_gmt":"2021-02-17T12:04:09","guid":{"rendered":"https:\/\/toolset.com\/?post_type=course-lesson&#038;p=1929573"},"modified":"2021-06-08T13:13:40","modified_gmt":"2021-06-08T13:13:40","slug":"how-to-create-custom-searches-and-relationships-for-users","status":"publish","type":"course-lesson","link":"https:\/\/toolset.com\/de\/course-lesson\/how-to-create-custom-searches-and-relationships-for-users\/","title":{"rendered":"How to Create Custom Searches and Relationships for Users"},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Artists who submit their paintings<\/li><li>Music composers who submit their songs<\/li><li>Contractors to be hired who submit the websites they have built<\/li><\/ul>\n\n\n\n<div class=\"wp-block-toolset-blocks-grid tb-grid\" data-toolset-blocks-grid=\"3d9c399fe625e1b972f817a0680229f6\">\n<div class=\"wp-block-toolset-blocks-grid-column tb-grid-column tb-grid-align-top\" data-toolset-blocks-grid-column=\"3034fbe886c11054e95b46b09d3e4112\"><div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"4896ff9329be66c41f2a79b570e249a2\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/list-of-contractors-search.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/list-of-contractors-search-500x296.png\" alt=\"\" class=\"wp-image-1929633\"\/><\/a><div class=\"tb-image-caption\"><figcaption>List of contractors with a custom search (see<a href=\"https:\/\/wpml.org\/contractors\/\">\u00a0live demo<\/a>)<\/figcaption><\/div><\/figure>\n<\/div><\/div>\n\n\n\n<div class=\"wp-block-toolset-blocks-grid-column tb-grid-column tb-grid-align-top\" data-toolset-blocks-grid-column=\"3034fbe886c11054e95b46b09d3e4112\"><div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"2fc7734966551b5f6c74becc55f858e2\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/individual-contractor.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/individual-contractor-500x353.png\" alt=\"\" class=\"wp-image-1929643\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Individual contractor page with his portfolio (see\u00a0<a href=\"https:\/\/wpml.org\/contractors\/bartek-stepniewski\/\">live demo<\/a>)<\/figcaption><\/div><\/figure>\n<\/div><\/div>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"support-for-users-in-toolset-and-wordpress\">Support for users in Toolset and WordPress<\/h2>\n\n\n\n<p>Using Toolset, you can&#8217;t create a custom search for&nbsp;<strong>user profiles<\/strong>.<\/p>\n\n\n\n<p><strong>WordPress itself<\/strong>&nbsp;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.<\/p>\n\n\n\n<p>However if you&nbsp;<strong>mirror user information<\/strong>&nbsp;into a&nbsp;<strong>custom post type<\/strong>, you can achieve what you need.<\/p>\n\n\n\n<p>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\u2014in our case, the Contractor.<\/p>\n\n\n\n<p>The image below illustrates this idea:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"f97ad134de768982bd0ee34c59615e2e\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/wordpress-profile-custom-post-type.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/wordpress-profile-custom-post-type-500x287.png\" alt=\"\" class=\"wp-image-1929659\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Separating public user data in a custom post type<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>So, how do you create a connection between user profiles and your post type?<\/p>\n\n\n\n<p>In WordPress, when a user creates a new post, a username is set in the post author field. This is when the \u201cconnection\u201d between the new post and the user profile is made.<\/p>\n\n\n\n<p>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&nbsp;<strong>logged-in<\/strong>. In this way, the relevant username will be saved with each new post in the&nbsp;<strong>post author<\/strong>.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\"><p>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 \u201cconnect\u201d WordPress profiles to a post type.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"structures\">Structures<\/h3>\n\n\n\n<p>Here are the structures you will need in order to implement the case with contractors and their showcases:<\/p>\n\n\n\n<p>1.&nbsp;<strong>WordPress User Profiles<\/strong><\/p>\n\n\n\n<p>Use WordPress user profiles to store the username and basic information about the user, such as e-mail, first and last names.<\/p>\n\n\n\n<p>2.&nbsp;<strong>Contractors&nbsp;<\/strong>\u2013 Custom Post Type for storing additional user information<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>3.&nbsp;<strong>Showcase<\/strong>&nbsp;\u2013 Custom Post Type for storing multiple instances of the user\u2019s work<\/p>\n\n\n\n<p>If you need to store more than one item representing the user\u2019s work\/portfolio,&nbsp;like Websites or Mobile Apps,&nbsp;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.<\/p>\n\n\n\n<p>Add custom fields to your Showcase post type.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"553e01c39b28ec4137b198c92cbf2d21\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/post-types-wordpress-user-profile.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/post-types-wordpress-user-profile-500x282.png\" alt=\"\" class=\"wp-image-1929687\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Post types and relationships with the user profiles set up by using the post author field<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>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.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"the-flow\">The flow<\/h3>\n\n\n\n<p>This is the flow you will need to handle:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A user registers with your site.<\/li><li>A WordPress profile (account) is created for him or her.<\/li><li>The user logs in.<\/li><li>User can use a front-end form to create Contractor profile now. <\/li><li>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.<\/li><li>The contractor can use a front-end form to submit work\/portfolio. The showcase entries become available to the public on the contractor\u2019s individual page and on the list of all showcase entries.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"forms-templates-and-views-to-create\">Forms, Templates, and Views to create<\/h2>\n\n\n\n<p>To handle the flow, you will need to:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Provide a way to create user profiles through the front-end (signup forms). You can create a &nbsp;<a href=\"https:\/\/toolset.com\/de\/course-lesson\/creating-forms-for-registering-users\/\">User Form using Toolset<\/a>.<\/li><li>Provide a way to allow your users to log in to the site (login forms). You can <a href=\"https:\/\/toolset.com\/de\/course-lesson\/creating-forms-for-registering-users\/\">Insert the registration form into a page<\/a>.<\/li><li>Create a Post Form to add new Contractors (<a href=\"https:\/\/toolset.com\/de\/course-lesson\/front-end-forms-for-adding-content\/\">use the Toolset Forms plugin<\/a>). Make this post form available only to logged-in users (<a href=\"https:\/\/toolset.com\/de\/course-lesson\/controlling-access-to-front-end-forms\/\">use the Toolset Access plugin<\/a>).<\/li><li>Create a&nbsp;Post Form&nbsp;to add new Showcase entries. Again, use Toolset Access to make sure that this post form is available only to logged-in users.<\/li><li>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 <strong>Content Selection<\/strong> section, and click <strong>Add a filter<\/strong>. Select the&nbsp;<strong>Post author filter<\/strong> and then the <strong>Post author is the author of the page where this View is shown<\/strong> option.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"123531b2d579409e24d11039aa09653a\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/view-with-the-post-author-filter.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/view-with-the-post-author-filter-500x723.png\" alt=\"\" class=\"wp-image-1931501\"\/><\/a><div class=\"tb-image-caption\"><figcaption>View with the post author filter<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<ol class=\"wp-block-list\" start=\"6\"><li>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.<\/li><li>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 <strong>Content Selection<\/strong> section, and click <strong>Add a filter<\/strong>. Select the&nbsp;<strong>Post author filter<\/strong> and then the <strong>Post author is the same as the logged in user<\/strong> option.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"c13d64d26bca69227d699a6811bce8b0\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/view-with-the-post-author-filter-2.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/view-with-the-post-author-filter-2-500x723.png\" alt=\"\" class=\"wp-image-1931515\"\/><\/a><div class=\"tb-image-caption\"><figcaption>View with the post author filter<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"how-do-i-prevent-users-from-creating-more-than-one-contractor-post\">How do I prevent users from creating more than one Contractor post?<\/h2>\n\n\n\n<p>Here\u2019s 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.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Create a page where the newly registered users can create their Contractor profile post.<\/li><li>Add a&nbsp;<strong>View<\/strong>&nbsp;that lists Contractor posts. Select the View block, in the right sidebar expand the <strong>Content Selection<\/strong> section, and click <strong>Add a filter<\/strong>. Select the&nbsp;<strong>Post author filter<\/strong> and then the <strong>Post author is the same as the logged in user<\/strong> option.<\/li><\/ol>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"85cd23e9eadca9690d7e531530f369d7\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/contractors-created-by-the-logged-in-user.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/contractors-created-by-the-logged-in-user-500x723.png\" alt=\"\" class=\"wp-image-1931525\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Contractors created by the logged-in user<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<ol class=\"wp-block-list\" start=\"3\"><li>Don&#8217;t add any blocks to the <strong>View Loop<\/strong>. Instead, select the <strong>View Loop<\/strong> block, in the right sidebar expand the <strong>Loop Template<\/strong> section, and add the following shortcode:<\/li><\/ol>\n\n\n\n\n                    <div class='codesnipdiv' id='7a061'>\n                    <a class='Insert contractor form' data-titleselector='insert-contractor-form' data-randspinner ='7a061' data-sniphost='1929573' title='Click to Expand or Minimize' id='codesnipfrontend'><img src='https:\/\/toolset.com\/wp-content\/plugins\/code-snips\/res\/img\/code_icon.png' \/>Insert contractor form<\/a>\n                    <div id='7a061_innerdiv' class='clearfix'>\n                    <pre class='brush:php;'>[cred_form form=&quot;your-form-slug&quot;]<\/pre>\n                    <script>\n                    if ('undefined' === typeof cacheObj_open) {\n                        window.cacheObj_open = {};\n                    }\n                    SyntaxHighlighter.highlight();\n                    <\/script>\n                    <input type='hidden' id='7a061_minimize' value='Minimize'>\n                    <input type='hidden' id='7a061_cachedstartopen' value='cachedstartopen'>\n                    <\/div>\n                    <\/div>\t\t\t\n                    <script>\t\t\t\t\t\t\t\n                    var insertname_outputs_startopen\t= '7a061_insert-contractor-form';\t \t\t\t\n                    cacheObj_open[insertname_outputs_startopen]='<pre class=\"brush: php;\">[cred_form form=&quot;your-form-slug&quot;]<\/pre>';            \n                    <\/script>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"1893b4ba747a4227f7d929b598d3acbb\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/insert-form-shortcode-1.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/insert-form-shortcode-1-500x273.png\" alt=\"\" class=\"wp-image-1931759\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Use shortcode to insert form<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<p>Replace <code>your-form-slug<\/code> with the slug of your form for creating contractor post type. You can find the form&#8217;s slug by editing it and looking at the top of the page.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\" tb-image tb-image-media aligncenter\" data-toolset-blocks-image=\"f53b4aedf67261eac8ee3b8131c18ca6\"><a href=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/form-slug.png\"><img decoding=\"async\" loading=\"lazy\" src=\"https:\/\/toolset.com\/wp-content\/uploads\/2021\/02\/form-slug-500x196.png\" alt=\"\" class=\"wp-image-1931777\"\/><\/a><div class=\"tb-image-caption\"><figcaption>Form slug<\/figcaption><\/div><\/figure>\n<\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"summary\">Summary<\/h2>\n\n\n\n<p>In Toolset, you can create relationships only between your post types, and these relationships should cover most typical cases.<\/p>\n\n\n\n<p>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&nbsp;<strong>post author<\/strong>&nbsp;field.<\/p>\n","protected":false},"author":56710,"featured_media":1957913,"template":"","related-lesson":[8469,8421],"class_list":["post-1929573","course-lesson","type-course-lesson","status-publish","has-post-thumbnail","hentry","related-lesson-post-relationships","related-lesson-custom-search"],"_links":{"self":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/1929573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson"}],"about":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/types\/course-lesson"}],"author":[{"embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/users\/56710"}],"version-history":[{"count":7,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/1929573\/revisions"}],"predecessor-version":[{"id":2080387,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/course-lesson\/1929573\/revisions\/2080387"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/media\/1957913"}],"wp:attachment":[{"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/media?parent=1929573"}],"wp:term":[{"taxonomy":"related-lesson","embeddable":true,"href":"https:\/\/toolset.com\/de\/wp-json\/wp\/v2\/related-lesson?post=1929573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}