Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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.

share|improve this question

2 Answers 2

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> 
share|improve this answer
    
I've made an edit to the original post, explaining more on my setup. I tried with get_field('testimonial') and I get the same result (nothing is being displayed). The code from above is in the main loop (but I also tried with it outside of the loop). It's the same. Should I be using 'relationship' instead of 'post_object'? –  George Grigorita Apr 24 '13 at 12:23
1  
I also edited my answer. In general both field types do the same. The relationship just has a more 'fancy' ui on the frontend. Have a look here: advancedcustomfields.com/resources/field-types/relationship –  s1lv3r Apr 24 '13 at 12:35
    
@GeorgeGrigorita, all observations by s1lv3r are correct. 1) Overriding of $post_object. 2) Using get_field inside or outside the loop. 3) Unnecessary foreach for a field with single value, it's not an array. –  brasofilo Apr 24 '13 at 13:53
    
Thanks all for helping! I really don't know why, but it seems to be working with 'relationship' as a field-type instead of 'post-object'. I'll update the first post. –  George Grigorita Apr 24 '13 at 14:45
    
I guess the reason it's now working is because relationship fields always return an array of post objects. –  s1lv3r Apr 24 '13 at 14:52

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.