Tell us what you are trying to do?
I am trying to filter a list of custom-post types inside a View Search, based on a multi-select ACF field.
Each custom-post has assigned 1 or more values in the post edit backend.
I need this field to display all available choices based on the view output, thus helping to filter choices.
The search field is outputting the ACF Field as array value instead of the HTML Select Options i hoped for.
Is there any documentation that you are following?
Did not find any.
Is there a similar example that we can see?
No
What is the link to your site?
hidden link
Hello and thank you for contacting the Toolset support.
Toolset Types and ACF store the custom fields differently. For simple fields, such as single line, or number fields, Toolset manages to make it work even if the fields are managed by ACF. But for advanced fields, such as select, checkboxes, etc. I wouldn't expect Toolset to search within them out of the box. Some custom code may be required.
So, let's see how we can make this work. For the search filter, I would suggest entering the possible values of the field in the block definition. Check this screenshot hidden link
This can be simple if you don't have multiple possible values for the field. But if there are a lot of options, I would suggest creating a custom code that will generate the dropdown. If that's needed, I can elaborate more about this option.
At this point, hopefully, we'll get the search filter to display as you would expect. But, I wouldn't be sure if the search will work in this field. If it does not work, we may need custom code to adapt the view's query. At that point, we'll need to know how ACF stores the value at the database level. But nothing guaranteed, maybe, we can't get it to work.
I hope this makes sense. Let me know if you have further questions.
Regarding the actual search, I found this article that explains a bit about how to adapt a WP_Query instance to search within multiple value fields. The trick is to use "LIKE" in compare key.
hidden link
And here is the Toolset hook to use, in order to adapt the view's query(WP_Query instance) https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Unfortunately the list options are more than 100, possibly more later.
The DB field for this custom post type has the slug 'citta_in_cui_si_trova' and it's type is ACF SELECT with multi-options enabled.
Data is stored as value : key in the array, like so:
afragola : Afragola
alessandria : Alessandria
altamura : Altamura
Is there a way to create a custom view filter as a dropdown select?
I can give you access to the site if you need.
Is there a way to create a custom view filter as a dropdown select?
I am sure there would be a way. But it will require custom code, which is out of the scope of the support. https://wpml.org/purchase/support-policy/
Create a custom shortcode that will generate the dropdown from the possible values of the ACF field. Check this article on how to create a custom shortcode https://codex.wordpress.org/Shortcode_API
And check this ACF support thread hidden link
It says that the "get_field_object" function can return the options for the select field.
hidden link
In order for the generated dropdown to be used by the view, it will need to have the class "js-wpv-filter-trigger". Check this screenshot hidden link
The generated select will also need to have a name attribute. This name attribute can be used in a query filter like in this screenshot hidden link
This may remove the need to have custom code hooked to the wpv_filter_query filter.
Hey,
i am trying to follow the shortcode route and created the following in functions.php:
function print_acf_select_field() {
if ( !is_admin()) {
ob_start();
$post_id = get_the_ID();
$field = get_field_object('field_60474e1847b97');
if( $field['choices'] ):
echo '<div class="wp-block-toolset-views-custom-search-filter wpv-custom-search-filter wpv-custom-search-filter-label-top">'
.'<div>'
.'<div class="form-group">'
.'<span class="wpv-custom-search-filter__input">'
.'<select id="wpv_control_select_citta_in_cui_si_trova" name="wpv-citta_in_cui_si_trova" class="js-wpv-filter-trigger form-control">';
foreach( $field['choices'] as $value => $label ):
echo '<option value="' . $value . '">' . $label . '</option>';
endforeach;
echo '</select>'
.'</span>'
.'</div></div></div>';
endif;
return ob_get_clean();
}
}
add_shortcode('select-field', 'print_acf_select_field');
Problem 1
Shortcode generates the field and AJAX updates when we choose options. BUT it outputs ALL the options possible on custom select field, even if they are not present on shown posts.
Problem 2
After we choose option, AJAX updates but Custom Select disappears.
Problem 3
Posts do not get filtered.
Possible cause:
I noticed that
in the function is outputting search form page ID instead of the ID's present in the search results.
Hope you can shed some light on this. Thank you.
The post ID idea maybe is not the option. How can we make the custom select behave like normal Tools Seach fields and populate based on posts present on the results?
My apologies for the late reply, but I do not work on Wednesdays and Thursdays.
I reached out to the team to get more tips and it seems that there is no way to resolve problem 1. Maybe using a Javascript code if you output the values of the field for each post in the view, the code will gather all the options of the results, and remove the non-existing options from the dropdown.
Otherwise, there is nothing we can help with it here. The field is not a Toolset one, and it is saved as a serialized array. Toolset does not save multi select field as serialized array, they are saved as raw values in multiple entries in postmeta table instead of one entry. In Toolset, there is support for non-Types custom fields as filters, but not when they are stored in a proprietary way (as with the ACF multi-select). Check this screenshot hidden link
Regarding problem 2, I would expect the shortcode to work also for AJAX. I need to check on a clean install and verify with our 2nd Tier if that's a bug. As a workaround, you may disable AJAX for the view.
Regarding problem 3, you need to hook into the view's filter as I suggested in this reply https://toolset.com/forums/topic/acf-select-field-as-view-search-field-outputs-array-instead-of-list/#post-1979831
I don't see, why you would need to use the get_the_ID(); inside the shortcode's code. And it is expected that it will return the page's ID. get_the_ID(); will return the post ID inside the view's loop not inside the filters.
Thanks for your time.
These problems were delaying the project too much so in the meantime we abandoned the functionality, and kinda side-stepped the problem by creating a taxonomy and assigning it to the post-type, this way views:search could filter results in a default fashion.
As your customer though, i would like to leave some comments:
We bought 3 site licenses from you mainly for the Maps add-on and the View:Search functionality.
I understand that the Views functionality was made to make the most out of your other plugins, but i believe you are missing out on sales for not integrating fully with ACF (we work solely on theme.co 's PRO and X themes and ACF PRO is bundled). Specifically the View:Search functionality would be AWESOME if it accounted for types of data stored by ACF.
We also tried to replace ACF with Toolset Types in a clean local installation to see if it could replace ACF for this use case.
But whenever we had to insert long lists of data into Toolset's SELECT or REPEATABLE fields, the fact that we had to insert by hand each value individually is a complete waste of time, please find a way to insert data similarly to ACF's Select fields.
We also found the WP BLOCKS interface quite clunky and slow to work within Views, even on a high end PC.
That is all for now, thank you.
Thank you for your feedback. I am sorry we couldn't help further with this request. And I think that taxonomy is probably a good fit for it as it will be more performant in searches than a custom field.