Support

Account

Home Forums Bug Reports Maybe error in documentation page

Solved

Maybe error in documentation page

  • Hi,

    in http://www.advancedcustomfields.com/resources/relationship/

    the setup postdata example is :

    
    <?php 
    
    $posts = get_field('relationship_field_name');
    
    if( $posts ): ?>
        <ul>
        <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
            <?php setup_postdata($post); ?>
            <li>
                <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
                <span>Custom field from $post: <?php the_field('author'); ?></span>
            </li>
        <?php endforeach; ?>
        </ul>
        <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif; ?>
    

    With ACF Pro 5+ , when i begin a page with my field (outside the main loop) the code doesn’t work

    it’s because you use $posts = get_field …
    $posts is overidded and the main loop is broken

    sometimes we need to use get_field outside the loop, to get datas from options pages

    It would be cool to add a little notice for this particular case ?

    calling $posts another way, like $relation_posts will work.

    Thanks

  • I will see what I can do about bringing this to the developer’s attention, but can’t guarantee anything will be done about it. Thanks for the feedback.

    I’m not the writer or maintainer of the documentation. The following are just my own tjoughts.

    Anywhere you see something like: $posts = get_field('relationship_field_name'); it should be assumed that this code needs to be inside of “The Loop”. The documentation here is only a reference to how to use things in the most general way.

    It would be extremely difficult for Elliot to include notes about all conditions that may arise, like putting the main WP loop inside a loop for a custom field. Something like this would cause errors and problems even without ACF where one post loop is nested inside another.

  • Yes I toldly understand. The doc would become a big great mess.

    I’ll just leave this post there. It could be useful maybe sometime for someone with the same problem. Or for the futur me ๐Ÿ˜‰

    Thanks for the answer.

  • Hi @maximebj

    Thanks for the topic.

    $posts is not a variable used by WP, so I wonder if you theme / plugins contain some code which is using $posts as well.

    I have used $posts may times in projects and had no issues – inside and outside of the WP loop.

    Thanks
    E

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

You must be logged in to reply to this topic.