Skip Navigation

[Resolved] Restricting View Filter to Specific Terms

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

Problem:

I am trying to restrict a view to a subset of values for a field called positions:

1) When user open the the page, Views are filtered by:
field "positions" value is one of below two constants:
applicant, supervisor

2) When user change the field "Positions" in custom search form, views are filtered by user's chosen option

Solution:

That is expected result:

Users can no longer use the filter to show just caregivers or just applicants.

within Views's UI, you can setup only one of above two filters, you can not setup both two filters.

As a work around, you can try these:

1) Restore back your view's filter settings, filter the view by URL parameter "wpv-wpcf-hr-position"

https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

2) When user open the page, use Views filter hook "wpv_filter_query" to trigger a PHP function

https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

In this PHP function, do these:

1) Check if there is URL parameter "wpv-wpcf-hr-position"
http://php.net/manual/en/reserved.variables.get.php

if not, add a custom filter to query, filter the query by:
field "positions" value is one of below two constants:
applicant, supervisor

Relevant Documentation:

https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

This support ticket is created 6 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.08 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 3 replies, has 2 voices.

Last updated by Luo Yang 6 years, 6 months ago.

Assisted by: Luo Yang.

Author
Posts
#789486
front-end-screen-shot.jpg
filter_screen_shot.PNG

I am trying to restrict a view to a subset of values for a field called positions. I also want to make available a filter which lets the user filter based on the subset of the values.

I have a post type hr-profiles with a select field which has one of the following values; applicant, caregiver, supervisor and manager.

I have made a view which only includes entries with position values of applicant or caregiver. I have my custom filter setup as shown in the attached screenshot. This successfully limits the results to just caregivers or supervisors. But this seems to break the filters on the page. Users can no longer use the filter to show just caregivers or just applicants. In addition the filter lists all of the possible values, not just caregivers and supervisors. Hopefully the screenshots help explain all of this.

My filters are setup as follows:

[wpv-filter-start]
<div class="row">
    <div class="col-sm-6">[wpv-control-postmeta field="wpcf-hr-position" url_param="wpv-wpcf-hr-position" default_label="Positions - All"] [wpv-control-postmeta field="wpcf-hr-status" url_param="wpv-wpcf-hr-status" default_label="Status - All"]</div>
    <div class="col-sm-6">&nbsp;</div>
</div><div class="form-group">
[wpv-filter-end]
#791116

Hello,

I assume we are talking about this case, you are going to add two filters on the same field "positions"
1) When user open the the page, Views are filtered by:
field "positions" value is one of below two constants:
applicant, supervisor

2) When user change the field "Positions" in custom search form, views are filtered by user's chosen option

If it is, that is expected result:
Users can no longer use the filter to show just caregivers or just applicants.
within Views's UI, you can setup only one of above two filters, you can not setup both two filters.

As a work around, you can try these:
1) Restore back your view's filter settings, filter the view by URL parameter "wpv-wpcf-hr-position"
https://toolset.com/documentation/user-guides/filtering-views-by-custom-fields/

2) When user open the page, use Views filter hook "wpv_filter_query" to trigger a PHP function
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

In this PHP function, do these:
1) Check if there is URL parameter "wpv-wpcf-hr-position"
hidden link
if not, add a custom filter to query, filter the query by:
field "positions" value is one of below two constants:
applicant, supervisor
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

#803580

I understand. Thank you for the tips and the alternate solution.

#807884

You are welcome