Value not loading on Posts page

Overview

When viewing an archive page, you may find that ACF is unable to load data from the page as expected. This is because an archive page modifies the WP Query so that the global $post is not longer a “page”, but a selection of post objects. This change to the global $post is what prevents ACF from loading data from the correct location.

This problem is most commonly experienced when changing the Reading Settings -> Front page displays setting to a static page, and then choosing a custom Posts page.

The problem

Here is some example code or a basic home.phptemplate file. This file is used to display the latest posts (Posts page setting). Please take note of the <h1> tag which should display a custom field on the page, however, this will always appear blank because (as described above) ACF does not know which page to load the data from.

<?php get_header(); ?>

	<h1><?php the_field('heading'); ?></h1>

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">
		<?php if ( have_posts() ) : ?>

			<?php /* The loop */ ?>
			<?php while ( have_posts() ) : the_post(); ?>
				<?php get_template_part( 'content', get_post_format() ); ?>
			<?php endwhile; ?>

			<?php twentythirteen_paging_nav(); ?>

		<?php else : ?>
			<?php get_template_part( 'content', 'none' ); ?>
		<?php endif; ?>

		</div><!-- #content -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

The solution

The simple solution is to tell ACF to load the data from the ‘Posts page’. You can do this by adding a second parameter to the get_field or the_field functions. This second parameter will contain the correct ID which can be found usingĀ  get_option('page_for_posts') like so:

<?php get_header(); ?>

	<h1><?php the_field('heading', get_option('page_for_posts')); ?></h1>

	<div id="primary" class="content-area">
		<div id="content" class="site-content" role="main">
		<?php if ( have_posts() ) : ?>

			<?php /* The loop */ ?>
			<?php while ( have_posts() ) : the_post(); ?>
				<?php get_template_part( 'content', get_post_format() ); ?>
			<?php endwhile; ?>

			<?php twentythirteen_paging_nav(); ?>

		<?php else : ?>
			<?php get_template_part( 'content', 'none' ); ?>
		<?php endif; ?>

		</div><!-- #content -->
	</div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>