Forums › Forums › Search & Filter Pro › Display ACF fields info for each result
Tagged: results
- This topic has 5 replies, 2 voices, and was last updated 5 years ago by Trevor.
-
Anonymous(Private) October 24, 2019 at 3:56 pm #224524
Can you give me a basic rundown of how to access/manipulate which ACF data is displayed?
My search has checkboxes for ACF checkboxes on an “OR” relationship, so it will return any results in which one or more of the checked boxes in the search, is also checked in the post itself.
I would like to display all the checked fields for each result, as well as some other ACF fields, such as a date field.
Example: If I have a custom post type “Dog” and each post has an ACF field with checkboxes like “large size” and “brown fur” and etc.
If I mark a checkbox in my search form for “brown fur”, I’d like to display all selected options for each given result; The results would return all Dog posts with “brown fur” checked, but I want to display their other checkbox selected options too. So if a Dog post is returned in “results” because it has “brown fur” checked in ACF, I’d like to list its other qualities as well.
TL;DR – how can I display more info from each post’s ACF fields in the results blocks?
Does this make sense?
Trevor(Private) October 24, 2019 at 7:50 pm #224575It does, but you need access to the PHP template that the page sues to display the results. If you are using our Shortcode display results method, then follow the guidance here:
https://searchandfilter.com/documentation/search-results/using-a-shortcode/#customising-the-results
There are many examples of accessing ACF field data on the ACF forums on StackExchange. One difference is that you should use
get_the_ID()
to fetch the post ID of each post in the loop, and no reset is used.This search on our forums shows posts with some snippets:
https://support.searchandfilter.com/forums/search/the_field/
Anonymous(Private) October 25, 2019 at 4:41 pm #224680I got it working by using the following code from this page: https://searchandfilter.com/documentation/search-results/custom/
`
$args = array(‘post_type’ => ‘post’);
$args[‘search_filter_id’] = 123;
$query = new WP_Query($args);
`
and then using “the loop” and grabbing the checkbox field for each post:
`
<?php if ( $query->have_posts() ) : ?>
<!– the loop –>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<!– get post tags field –>
<?php $checkbox_fields = get_field(“checkbox_field_name”); ?>
`
Then running a foreach on
$checkbox_fields
to display them or a certain amount of them for each result.Posting this here so people can use this in the future if they want to better control how their results are displayed.
Anonymous(Private) October 25, 2019 at 4:42 pm #224682$args = array('post_type' => 'post'); $args['search_filter_id'] = 123; $query = new WP_Query($args);
and
<?php if ( $query->have_posts() ) : ?> <!– the loop –> <?php while ( $query->have_posts() ) : $query->the_post(); ?> <!– get post tags field –> <?php $checkbox_fields = get_field("checkbox_field_name"); ?>
did my code markup wrong there whoops
-
AuthorPosts