The forums are closed and will be removed when we launch our new site.
Looking for support? You can access the support system via your account.
Forums › Forums › Search & Filter Pro › Output ACF Post Object fields in results
Tagged: acf, post object, post_object, search results
- This topic has 6 replies, 2 voices, and was last updated 7 years, 5 months ago by Trevor.
-
mike distras(Private) April 7, 2017 at 3:04 pm #101976
Hi,
Im unable to get ACF Post Object fields to output in to the results, any idea’s on the correct format?<?php while ($query->have_posts()) { $query->the_post(); ?> <tr> <td> <?php $post_object = get_field('customer'); if( $post_object ): // override $post $post = $post_object; setup_postdata( $post ); ?> <?php the_title(); ?> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?> </td> <td><?php the_date(); ?></td> <td><?php echo the_ID(); ?></td> <td><?php echo the_field('reg_no'); ?></td> </tr> <?php } ?>
At the moment “the_title” inside the post object, outputs the title of the search result, not the title thats defined inside the search result post.
Hopefully that makes sense, and hopefully its just a syntax issue.
Thanks,
Mikemike distras(Private) April 7, 2017 at 3:06 pm #101977Sorry for the badly formatted code, cant see where to edit my post. Here it is formatted a little more readable:
<?php while ($query->have_posts()) { $query->the_post(); ?> <tr> <td> <?php $post_object = get_field('customer'); if( $post_object ): // override $post $post = $post_object; setup_postdata( $post ); ?> <?php the_title(); ?> <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?> <?php endif; ?> </td> <td><?php the_date(); ?></td> <td><?php echo the_ID(); ?></td> <td><?php echo the_field('reg_no'); ?></td> </tr> <?php } ?>
Trevor Moderator(Private) April 7, 2017 at 4:40 pm #102009A lot of that you don’t need. You need to be careful of the type of field you are getting. For example, arrays are different. The ACF online manual is a big help. Below is an example results.php file that gets ACF fields and uses them:
<?php /** * Search & Filter Pro * * Sample Results Template * * @package Search_Filter * @author Ross Morsali * @link http://www.designsandcode.com/ * @copyright 2015 Designs & Code * * Note: these templates are not full page templates, rather * just an encaspulation of the your results loop which should * be inserted in to other pages by using a shortcode - think * of it as a template part * * This template is an absolute base example showing you what * you can do, for more customisation see the WordPress docs * and using template tags - * * http://codex.wordpress.org/Template_Tags * */ if ( $query->have_posts() ) { ?> Found <?php echo $query->found_posts; ?> Results<br /> Page <?php echo $query->query['paged']; ?> of <?php echo $query->max_num_pages; ?><br /> <div style="width:100%;"> <?php while ($query->have_posts()) { $query->the_post(); ?> <div class="result"> <?php if (get_field('property_main_image') != '') { ?> <img />" alt="" width="400px" /> <div class="status"><?php echo get_field('status'); ?></div> <div class="details"> <?php echo '<h4>$' . get_field('price') . ' </h4>'; echo '<p>' . get_field('address') . '<br>'; echo get_field('city') . ', '; echo get_field('state'); echo get_field('zip') . '</p>'; echo '<p class="spex">' . get_field('bedrooms') . ' BEDROOMS | '; echo get_field('bathrooms') . ' BATHROOMS<br>'; echo '<span class="secondline">' . get_field('square_footage') . ' SQ FT | '; echo get_field('garages') . ' CAR GARAGE</span>'; echo '<span>MLS# ' . get_field('MLS') . '</span></p>';?> <div class="linx"> <a href="#"><img src="/CreativeHomes/wp-content/uploads/2016/08/CH_Button_Photos_Grey.png" alt="see photo gallery" /></a> <a target="_blank">"><img src="/CreativeHomes/wp-content/uploads/2016/08/CH_Button_MapPin_Grey.png" alt="google map for location" /></a> <a target="_blank">"><img src="/CreativeHomes/wp-content/uploads/2016/08/CH_Button_Collateral_Grey.png" alt="brochure download" /></a> </div><!--end linx--> <a>"><img src="/CreativeHomes/wp-content/uploads/2016/08/details-button.png" width="122" alt="details" /></a> </div><!--end details--> <div class="interact"> <span><?php the_favorites_button($post_id, $site_id); ?></span> <span><img src="/CreativeHomes/wp-content/uploads/2016/08/check-icon.jpg" />Compare</span> <span><?php if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>Share</span> </div><!--end interact--> </div><!--end result--> <?php } ?> </div> <?php } else { echo "No Results Found"; } ?>
mike distras(Private) April 7, 2017 at 4:50 pm #102011Great stuff, how come you are echoeing get_field instead of using the_field?
The example you provided outputs single fields fine, but what would be some example code to use ACF’s Post Object?
Using the example ACF site supplies doesnt work as expected.
Trevor Moderator(Private) April 7, 2017 at 5:17 pm #102017I think there is a subtle difference between echoing it and the other. Not sure.
As to Post Object, that is harder. We would have to spend some time over Skype together experimenting to get the code right, I think.
-
AuthorPosts
The topic ‘Output ACF Post Object fields in results’ is closed to new replies.