Display post_object content using Advanced Custom Fields plugin

I have a hard time figuring out what’s wrong with this code.

What I’m trying to do: setup up a custom field in admin using the ACF plugin to lists in a meta-box on all pages a list of testimonials (custom-post-type). From the drop-down menu the client can select a testimonial that will show up on that specific page.

Read More

I’m using a slightly modified version of twentytwelve theme and this is the code I’m using in my front_page template.

The code:

<?php $featured_testimonial = get_field('testimonial', $post_object->ID); ?>
    <?php foreach ($featured_testimonial as $post_object): ?>
            <a href="<?php echo get_permalink($post_object->ID); ?>">
                <p class="descr"><?php echo get_the_title($post_object->ID) ?></p>
            </a>
    <?php endforeach; ?> 

The problem: this code doesn’t show anything on the front-page. I did a print_r( $post_object ) and it showed all the info in my testimonial (title, date, content etc).

The solution: For the last 2/3 days I’m trying to get an answer on the ACF support forums and this is the reply I got:

The post object field will return a post object, not HTML. using
the_field on a post_object field will result in nothing being
displayed.

You need to store it as a variable, then use the ID or post_title
variable to output the desired HTML

Did I not understand the solution or I’m actually doing exactly what the message says and the problem is elsewhere?

Later edit: This is my current setup in ACF for the testimonial field:

Rules: Post Types - Page.
Field Name - Testimonial.
Field Type: Post Object.
Post type: Testimonial.
Filter from taxonomy: All.

The ‘rules’ means that the custom field will only appear on pages, and it will only list ‘post objects’ from the ‘testimonial’ custom post type. In admin everything seems to be working fine, it’s just that I can’t get anything to show up on the front-end.

Final edit: Following the suggestion of @s1lv3r I changed the field-type from ‘post-object’ to ‘relationship’. I’ve also modified the code like this:

<?php $posts = get_field('testimonial'); ?>
    <?php if( $posts ): ?>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <?php echo get_the_excerpt(); ?>... <a href="<?php the_permalink(); ?>">+More</a>
        <?php the_title(); ?>
    <?php endforeach; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>

Now everything works OK on the front-end. Thanks everyone for helping.

Related posts

Leave a Reply

3 comments

  1. The code doesn’t seem syntactical wrong in the first place. What kind of field type are you using? Relationship?

    Also why are you overriding $post_object and where does it come from in the first place? That part of the code is missing.

    To get the field which is attached to the current post (inside the current loop / global $post object you are viewing) you simply have to use get_field('testimonial') without the ID param.

    If you want to get the field attached to another post_object did you check that $post_object->ID contains an valid post_id right before the first line you posted?

    Edited according to new info:

    Is this a post object field with the ‘multiple’ option enabled? The docs (http://www.advancedcustomfields.com/resources/field-types/post-object/) say that this fields only returns an array of post objects when the multiple option is enabled otherwise it will only return a single post object. This would mean that you won’t need the foreach-loop in your code. In that case you should use it like this:

    <?php $featured_testimonial = get_field('testimonial', $post_object->ID); ?>
                <a href="<?php echo get_permalink($featured_testimonial->ID); ?>">
                    <p class="descr"><?php echo get_the_title($featured_testimonial->ID) ?></p>
                </a> 
    
  2. This code will be help

    <?php
    $featured_testimonial = get_field('testimonial', $post_object->ID); 
    ?>
    <a href="<?php echo get_the_permalink( $featured_testimonial->ID ); ?>"><?php echo $featured_testimonial->post_title; ?></a>
    
  3. First things first. Did you select a value from your drop-down on the admin side? If you haven’t then you will not get any results from get_field for that page.

    Also, if you have access to your database make sure you have a value for that page set in _postmeta table.

    SELECT * FROM `wp_postmeta` WHERE `post_id` = XX
    

    Where XX is your page ID, and YY (see below) is the ID of the testimonial you’ve picked from the drop-down on the admin side.

    Amongst the results you should have a couple of rows like this:

    meta_id    post_id    meta_key        meta_value
    1607       XX         testimonial     YY 
    1608       XX         _testimonial    field_5177d30141f17
    

    If you have all those properly set in the DB then it’s your code that’s the issue.