Skip Navigation

[Resolved] Dynamically set the filter options in a View filter

This support ticket is created 5 years, 10 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 4 replies, has 2 voices.

Last updated by KentS9937 5 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1190703

I have a View with a filter on field reviewer-1. I want to dynamically set the options for that filter. I can't figure out what hook to use to do this.

#1190741

Hi, do you mean you want to specify the filter options that are available (like in a front-end search filter select field) or you want to dynamically pre-define filter configurations (without any front-end search filters)?

To programmatically modify a query with PHP, we offer the wpv_filter_query API:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
This is for predefining filter configurations.

You can also dynamically set filters using shortcode attributes:
https://toolset.com/documentation/user-guides/passing-arguments-to-views/
This is also for predefining filter configurations, but does not require PHP.

Additionally, you can configure some of View's Query Filters to respond to URL parameters. Normally this is how front-end parametric searches work. In these cases, the best way to dynamically set those filter values is by using URL parameters.

If you give me some more details about what you're trying to accomplish, I might be able to offer more specific advice for your scenario.

#1190771

We have a View where we have defined filtering options. For some of the filter options we can define the option's values and display values using Toolset's "new filter" capability.

In 2 of the options we want to populate values and display the values programatically.

We are doing this now for those fields in the Form, but would like to have the View filterable by those fields.

The code we use for the Form is:

add_filter( 'wpt_field_options', 'sc_populate_reviewer2_field', 10, 2 );

function sc_populate_reviewer2_field( $current_options, $title_of_field ){

if ('Reviewer 2' == $title_of_field ) {
$current_options = array();
$args = array(
'role' => 'editor',
'orderby' => 'user_nicename',
'order' => 'ASC'
);
$current_options[] = array(
'#title' => '- none -',
'#value' => 0
);
$users = get_users( $args );
foreach ( $users as $user ) {
$current_options[] = array(
'#title' => $user->display_name,
'#value' => $user->ID
);
}
}
return $current_options;
}

#1190791

Okay if I understand you correctly, you have a View that allows front-end filtering based on a custom field. That field has multiple options (like a select field, checkboxes group, or radio field) and you would like to be able to programmatically modify the available filter options. Similarly you have programmatically modified those field options in a Form using the wpt_field_options function. Correct?

If so, unfortunately the short answer is it's not possible like this in the current system. Programmatically setting field options and filter options is not officially supported. The wpt_field_options function is not documented in our API documentation, despite other tickets in the forum that mention it. I can't recommend using it because it inevitably breaks custom searches that use the same custom field, and we don't have any viable solutions for resolving that problem with PHP or JavaScript APIs. So unfortunately this is a limitation of the current system. Field options must be predefined in the custom field editor screen. If not, filtering based on those field values in a custom search is broken in the current system. There are no available solutions to that particular problem.

Another option to consider is replacing this custom field with a basic text custom field. With Forms, you can use backend code to validate the input with the Forms API. Then in a View, you can filter upon this field using a "select" filter input, which displays all the possible options. In other words, you can achieve indirect programmatic control of the field options by validating and enforcing input rules. Or instead of all possible options, you can manually set the options for a "select" filter input in wp-admin (not programmatically, though). Let me know if you'd like to explore that approach more.

#1191320

Thank you. We'll find another way to attack this problem.