Skip Navigation

[Resolved] Parametric Search on Parent and Child custom fields

This thread is resolved. Here is a description of the problem and solution.

Problem:

I want to provide the user with two drop-down select boxes - one which filters on a child custom field, and one which filters on a parent custom field.

Solution:

There isn't such a built-in feature within Views plugin, see details here:

https://toolset.com/forums/topic/parametric-search-on-parent-and-child-custom-fields/#post-1229171

Relevant Documentation:

This support ticket is created 5 years, 7 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 1.57 hours from now. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 2 replies, has 2 voices.

Last updated by neilR 5 years, 7 months ago.

Assisted by: Luo Yang.

Author
Posts
#1229076

I want to provide the user with two drop-down select boxes - one which filters on a child custom field, and one which filters on a parent custom field.
e.g. Author CPT (parent) has custom select field "Nationality" with options GBR, GER, FRA, USA. Book CPT (child) has custom select field "Genre" with options Thriller, Romance, SciFi. I want to provide two dropdown filter controls to the user, one for each, to display a list of Books which meet the criteria, e.g. user can select Books where Author Nationality=GBR and Book Genre=Thriller.

Is there any documentation that you are following?
I understand that the best way to show Books where Nationality=GBR and Genre=Thriller is to have a View in a View, and I have implemented this successfully. e.g. https://toolset.com/forums/topic/search-by-parents-and-grandparents-fields/ and https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/ . However, this doesn't cover the issue of filter controls.

I can provide a Nationality filter control on the Parent View, but if I add a Genre filter control on the Child View, I get multiple identical dropdowns, one per Author, and each one only filters within the results for one Author. I also tried inserting only the layout in the results and the filter elsewhere on the page, but that was completely broken.

Is there a way of creating a control which will feed its parameter to every instance of the Child filter? Possible options that spring to mind are
(a) a control in the Parent View which somehow passes the parameter to all of the child views;
(b) displaying the child Genre filter only on the first instance of the child View, and somehow passing the parameter to other instances of the view,
(c) manually create a select box, then use conditional output to take the value in that box and alter output depending on whether the criterion is met https://toolset.com/documentation/user-guides/conditional-html-output-in-views/conditional-output-based-on-post-relationships/ - however, I'm unclear how I'd create that box (a control...?) and pass the parameter to the conditional statement. And of course, I wouldn't get the neat 'show only available values' in the dropdown...

Or is there a better way entirely?

Suggestions...?
Thanks,
N

#1229171

Hello,

There isn't such a built-in feature within Views plugin,
1) a control in the Parent View which somehow passes the parameter to all of the child views;
It needs a nested view, it seems that you have already done.

2) displaying the child Genre filter only on the first instance of the child View, and somehow passing the parameter to other instances of the view.

It is possible with custom CSS/JS codes, for example you can display first instance of child view custom search form, and hide other search form, see similar thread:
https://stackoverflow.com/questions/2717480/css-selector-for-first-element-with-class/8539107#8539107

And I suggest you disable AJAX search feature in child view custom search form, so after user submit the first child search form, it will pass the URL parameter to all views, and it will take effect on all child views

3) manually create a select box, then use conditional output to take the value in that box and alter output depending on whether the criterion is met

I don't think it is a good idea, [wpv-conditional] shortcode do not works for field in custom search form.

In my opinion, you might consider move all custom fields("Nationality" and "Genre") into child post type, then it will be easy to setup the custom search form:
https://toolset.com/documentation/user-guides/front-page-filters/advanced-settings-custom-search/

#1230102

Hi Luo,
I figured that was the case.
Thanks for confirming.
N