Advanced Custom Fields scoping issue with Sage

I’ve posted this on StackOverflow so will also paste the same here:

I’m using Roots Sage 9 inside WordPress and utilising the FrontPage.php controller which already has protected $acf = true; defined in it which is working okay, it outputs what I expect it to for the most part.

In my ACF configuration for the front page I have a relationship field (portfolio_items) which pulls through posts from a custom post type called portfolio and inside here are a number of additional ACF fields.

Now, the issue I’m having is what I can only imagine is something like ACF’s scope. For things defined inside the front page ACF fields the whole protected $acf works fine, but inside my foreach I’m trying to access ACF fields defined inside the custom post type and I just can’t get them to out put the info. For example, I have a text field defined called “archive title” and I’d usually output it with the_field('archive_title') but as I’m trying to use controllers I’d tried to use it as $archive_title with no luck, I just get “Notice: Undefined Variable: archive_title”

Here’s the full code I’m using at the minute:

    <?php global $post ?>
    @foreach($portfolio_items as $post)
        @php(setup_postdata($post))
        @php($thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "xlarge" ))
        @php($thumbnail_image = $thumbnail[0])
        <a href="{{ the_permalink() }}" class="col-md-3 direction-reveal__card">
            <img src="{{ $thumbnail_image }}" alt="Image" class="direction-reveal__img">
            <div class="direction-reveal__overlay direction-reveal__anim--enter">
                <h3 class="direction-reveal__title mt-auto">{{ $archive_title }}</h3>
            </div>
        </a>                
    @endforeach
    <?php wp_reset_postdata();  ?>

I can’t think of a way to fully access the other fields correctly. Has anyone come across this at all?

<h3 class="direction-reveal__title mt-auto">{{ get_field('archive_title', $post->ID) }}</h3>

IIRC protected $acf = true; will only pull in ACF data from the “current” page; it doesn’t magically pull in ACF data from any page you reference in a Controller. In order to get ACF content from posts in a relationship field, you’ll have to query that data manually, as above.

1 Like

This topic was automatically closed after 42 days. New replies are no longer allowed.