Forums Forums Search & Filter Pro Display ACF fields info for each result

Tagged: 

Viewing 6 posts - 1 through 6 (of 6 total)
  • Anonymous
    #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?

    Anonymous
    #224525

    *I have done a search in these forums for examples, but couldn’t find what I’m looking for. If you can link me to a post with the answer, I’m okay with that.

    Trevor
    #224575

    It 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
    #224680

    I 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
    #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

    Trevor
    #224686

    Thanks for letting me know. I will close this thread for now.

Viewing 6 posts - 1 through 6 (of 6 total)