Skip Navigation

[Resolved] Create search page and allow filtering by multiple relationship types

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Author
Posts
#2240985
Screenshot (69).png

Tell us what you are trying to do?
I am trying to set up a search/filtering page with a text search box, as well as some filters for the results. I have a CPT of HCCNs set up, with multiple relationships (EHR, EBHR, PHM, etc). Each of these many-to-many relationships has a set of custom fields. I would like to set up a page where the user can filter these down by which posts they are connected to. I understand it will involve quite a bit of coding, and that is the part I understand! What I don't understand is how to make it work with Toolset.

Is there any documentation that you are following?
Right now, I am trying to do this on the HCCN Archive page, but I am open to creating it on another page if that would be easier. I plan to create shortcodes https://codex.wordpress.org/Shortcode_API to filter the relationships in the functions file or perhaps a plugin, and then modify the query. I'm having some trouble getting started. https://toolset.com/forums/topic/adding-multiple-relationship-filters-to-archive-page/

https://toolset.com/course-lesson/creating-a-custom-search/#how-to-search-by-post-relationships: to set up the search page, but I'm not sure where to insert a shortcode that I create

What is the link to your site? This is the archive page I have started, but haven't gotten very far on:
hidden link Password: health
I'm happy to set you up as an admin if it helps you understand where I am

#2241259

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

This particular requirement is quite different and more complex than what was discussed in the other ticket with Jamal.

In that ticket, Jamal guided how multiple relationship search filters can be included, and here you need a single page where custom fields from multiple relationships (intermediary posts) can be used for filtering.

The way WordPress meta query works, it will skip all the posts where the custom field that is being used for the filtering doesn't exist.

For example, suppose you have two post types:

1. Shops
2. Books

If you'll create a view to show posts from both these post types, it will show the complete list. But, if you'll include a search/query filter for a custom field, e.g. "Shop Location", that is attached to the "Shops" posts, but not with the "Books" posts, then all "Books" posts will automatically be skipped/ignored.

Keeping this limitation in mind, I'm afraid, I can't think of a practical way to make this all-inclusive filtering work from within the same view or archive, even with code customization. Here is how I would achieve something similar, as a workaround:

1. I would create separate post views for each relationship connection (intermediary post types) and each view will only hold the custom field filters for the respective intermediary post type.

2. On a new page, I would add all these new views and hide them when the page loads.

3. On the top of the same page, I would add a custom select field, that will show all the relationship connections, as an option, so that the visitor can select which type of filter they plan to use.

3. Using some custom script, I would show only the view for the selected option, so that visitors can see and interact with that particular view's search fields and results. To use the search fields from the different relationships, the visitor can change the selection from the top and the respective view and its search fields will become available.

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#2242539

Waqar,
Thank you and I like your plan for a workaround. I have set up a page to test the EHR search. The first part works great (The select drop-down to filter by EHR) but then I'm having trouble filtering by custom fields. I think I may know why, but I'm not sure how to fix it. We are displaying HCCNs in the search results, and filtering by EHRs (another CPT) connected in a many-to-many relationship with HCCNs.
Then, the custom fields we also want to filter by (for example, "General Support, PHC Support") are fields on the EHR Relationship post that is created by the user. What am I missing here that is not allowing the filter to happen?
Here is the beginning of the page I created with the block editor. I also set up a view in the classic editor because I thought it might be easier to work with, but I'm having a similar problem there. I can ultimately do this on either the classic or block editor if one would be easier to accomplish what we're looking to do.
hidden link password: health

I've found some documentation that is related but still not sure:
https://toolset.com/forums/topic/filter-view-by-post-relationship-field-taxonomy/

This one has some custom code that may be useful, but I am trying to see if we need to go this route or I can do it within the search/view capabilities:
https://toolset.com/forums/topic/creating-a-multi-condition-andor-custom-filter-in-a-view/

#2243369

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Can you please share temporary admin login details, so that I can see how this search page is set up in the backend?

I'll be in a better position to guide you with the next steps accordingly.

Note: Your next reply will be private and it is recommended to make a complete backup copy, before sharing the access details.

#2245211

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thank you for sharing the admin access.

I think there is some confusion around what I suggested as a workaround and I've created a test page "Test page from TS support" to make it more clear.
( {yourwebsite}/test-page-from-ts-support/ )

Note: In this example, I'm using containers for 4 different views which include their own custom field filters, but you can change that as needed.

1. In an HTML block, I included the HTML for a select field, so that the visitor can select the type of filters to use:


<label for="view-select">Select a view:</label>
<select name="view-select" id="view-select">
	<option value="">Select a view</option>
	<option value="1">View 1</option>
	<option value="2">View 2</option>
	<option value="3">View 3</option>
	<option value="4">View 4</option>
</select>

2. Next, I added 4 container blocks, and included class "container-view" to all of them and unique IDs "container-view-1", "container-view-2", "container-view-3", and "container-view-4", to each one, respectively.
( screenshot: hidden link )

3. After that, I added another HTML block, that includes some custom CSS and JS code:


<style>
#container-view-1,
#container-view-2,
#container-view-3,
#container-view-4 {
display:none;
}
</style>

<script>
jQuery(document).ready(function( $ ) {
 $('select#view-select').on('change', function() {
  if( this.value > 0) {
   $('.container-view').hide();
   $('#container-view-'+this.value).show();
  }
  else
  {
   $('.container-view').hide();
  }
 });
});
</script>

The CSS code ensures that the containers for all views are hidden, when the page loads, and the JS code controls which view's container should be shown, when its relevant option is selected in the select field.

In this example, I've just included some text in each view's container. But on your actual search page, you can include a classic view in each container, with its respective search filters.

As a result, based on user selection, different views with different search filters will become available on the same page.

#2245575

Thank you, Waqar. I think I was starting from the other end (Individual views) and moving to the general search from there, but this is also a good way to go. hidden link pw: health

I have the select box loading on the page, and views in place for the different systems. What I'm missing here is this:
On the individual views (EHR, for example--"Filter EHR" in Classic Views) I'd like for there to be another filter where users can select Services offered. That is in a Custom field, which is on the EHR Intermediary post (entered by users). The first select box chooses from EHRs with an EHR Intermediary connection. I'm not having any success adding the Custom fields to the filter. An example would be "General Support--PHC Support." I can get it to show up as a checkbox, but then nothing actually filters.

#2245807

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for writing back.

Please allow me to reiterate the point from where this discussion started.
( ref: https://toolset.com/forums/topic/create-search-page-and-allow-filtering-by-multiple-relationship-types/#post-2241259 )

In post view, you can only use the search or query filter for the custom fields, that are directly attached to the post type, that is being shown by the view.

The view "Filter EHRs" ( ID: 4239 ), is set to show the "HCCNs" post type. This means that the search or query filter for the custom fields will only work for the custom fields attached directly to the "HCCNs" post type.

To use the search filters for the custom fields coming from the intermediary post type "ehr-relationship", you'll need a separate view that is set to show the "ehr-relationship" post type.

#2246239

I just want to make sure I'm explaining this accurately:

I still want to display HCCNs that are in an intermediary relationship with an EHR, in which the intermediary relationship contains a custom field. For example, Breakwater Health (HCCN) is in an EHR Intermediary relationship with Athena (EHR). The Intermediary relationship contains the custom fields:
General Support
Custom Report Development
Data Integration
Data Interface Development
Data Interface Implementation
End User Training
Helpdesk / Troubleshooting Support
Onboarding Training
etc.

I'd like the user to be able to filter or sort by these custom fields in addition to being able to select an EHR that the HCCN is in an intermediary relationship with.
So on the user end: User selects EHR, then user selects Custom Report Development. They would see HCCNs that are in an intermediary relationship that contains the custom field "Custom Report Development."

#2246419

I created a new view with EBHR Intermediary posts, which displays the name of those posts, but I'm trying to figure out how to integrate this with the rest of the search.
hidden link

#2248627

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

To use the search fields from the intermediary post type, you'll need a view similar to the one I created on the page:
hidden link

View: "Example view to show the use of intermediary post type and fields":
hidden link

1. You'll note that in the content selection, the intermediary post type "EHR Relationships Intermediary Posts" is selected, because we need to add search field filters for the fields, which are attached to this post type.
( ref: hidden link )

2. Next, in the loop of this view, I've included the shortcodes to show the titles of the related HCCN and EHR posts, to make it clear that even in the loop of the intermediary post type, you still have the ability to show the information from the related parent and/or child post types:


HCCN: [wpv-post-title item="@ehr-relationship.child"]<br>
EHR: [wpv-post-title item="@ehr-relationship.parent"]<br>

3. In the "Search and Pagination" section, I included the post-relationship filter so that the visitors can filter based on the parent post type "EHR".


[wpv-control-post-relationship ancestors="ehr@ehr-relationship.parent" url_param="wpv-relationship-filter"]
<div class="form-group">
	<label for="wpv-relationship-filter">[wpml-string context="wpv-views"]EHRs[/wpml-string]</label>
	[wpv-control-post-ancestor type="select" default_label="-select-" ancestor_type="ehr@ehr-relationship.parent"]
</div>
[/wpv-control-post-relationship]

4. To show how the custom field filter works, I also included the field filter for the "General Support: PHC Support" field.


<div class="form-group">
	[wpv-control-postmeta field="wpcf-general-support-phc-support" url_param="wpv-wpcf-general-support-phc-support"]
</div>

Note: I've included this field's value in the loop output, so that you can see whether the filtering is working or not.


General Support: [types field='general-support-phc-support'][/types]<hr>

Similarly, you can include the search field filters for the other fields from this intermediary post type.
( ref: hidden link )

As a completed result, if you'll select an EHR in the search field, for example, "Athena", you'll see only those results which are related to this EHR. And, if you'll also select the checkbox for the "General Support: PHC Support", you'll see that results will be further narrowed down to the ones where this field is checked.