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.
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.
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;
}
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.
Thank you. We'll find another way to attack this problem.