Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › Problem with acf relational field used as filter › Reply To: Problem with acf relational field used as filter
February 16, 2021 at 7:35 pm
#31605
Participant
Good 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;
}