Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Problem with acf relational field used as filter
This topic contains 5 replies, has 2 voices, and was last updated by Ernest Marcinko 2 years, 1 month ago.
- AuthorPosts
- February 16, 2021 at 10:12 am #31579
Good evening,
I have a problem with a custom field used as filter in my search. It is a acf custom field, and it is a relational field.
In backend I configured field as others custom fields, but in front-end results are non properly rendered. I attach screenshot of configuration and front-end problem. Others acf fields that I used as filters are ok, but they are not relational fields.
I have the site on my local pc, so I can not provide an access to backend or ftp for now.
Thank youFebruary 16, 2021 at 10:36 am #31581Hi,
The problem is, that relationship and repeater fields cannot be used as filters – only text based fields, such as input, email, number, radio, dropdown, checkbox etc.. Relationshop/repeater fields does not store information directly, and we cannot utilize them unfortunately.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
February 16, 2021 at 10:41 am #31583ok, that is a big problem for me. Could you indicate me part of code that I could try to edit (edit the function {get_values}) to handle relational fields? Thank you
February 16, 2021 at 1:46 pm #31593Sure, but getting the values printed is not the biggest issue. Filtering the actual results via the search query is what makes this almost impossible.
How I would recommend doing this is using the filters API to create new filters of your choice. That is for the frontend.
When creating the filter, choose a non-existent field name, so the plugin does not try to resolve it – as it would result in no results whatsoever. You can recognize this field in the next section, when you treat the actual results. Also, make sure you have this option enabed, so missing fields does not empty the result set.Once you have the filters you need to treat the results. Instead of trying to make a custom query I would recommend removing the results within the post process via the asp_results filter. There you have access to the initial query arguments via the $args variable (4th argument). The filter values passed on are stored in the
$args['post_meta_filter']
(referenced here).
With that information you can loop through the results and perhaps remove the ones that does not fit the query.However I can not guarantee this is going to work, this is not an easy thing to implement by any means.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
February 16, 2021 at 7:35 pm #31605Good evening,
I want to share my solution, thank you for your suggestions.
In configuration of plugin, I removed only “{get_values}”, but I leaved correct custom field name.
Than, simply I did populate options thanks to correct hook. In this way I dont need to manipulate results, because they remain alredy correct:add_filter('asp_pre_get_front_filters', 'asp_change_a_filter', 10, 2); function asp_change_a_filter($filters, $type) { foreach ($filters as $k => &$filter) { if ( $filter->type() == 'custom_field' && $filter->field() == 'recensore_aurotore_oblio' ) { $args = array( 'post_type' => 'recensorieautori', 'post_status' => 'publish', 'posts_per_page' => 999, 'orderby' => 'title', 'order' => 'ASC', ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); $filter->add(array( 'label' => get_the_title(), 'selected' => false, 'value' => 'a:1:{i:0;s:3:"'. get_the_id() .'";}' )); endwhile; wp_reset_postdata(); } if ( $filter->type() == 'custom_field' && $filter->field() == 'numero_rivista' ) { $args = array( 'post_type' => 'numeridellarivista', 'post_status' => 'publish', 'posts_per_page' => 999, 'orderby' => 'title', 'order' => 'ASC', ); $loop = new WP_Query( $args ); while ( $loop->have_posts() ) : $loop->the_post(); $filter->add(array( 'label' => get_the_title(), 'selected' => false, 'value' => 'a:1:{i:0;s:2:"'. get_the_id() .'";}' )); endwhile; wp_reset_postdata(); } } return $filters; }
February 17, 2021 at 1:11 pm #31612This is actually really great – I might do a knowledge base article based on your solution, really great job.
Best,
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
- AuthorPosts
The topic ‘Problem with acf relational field used as filter’ is closed to new replies.