ACF – get_field() using ‘option’ as post_id not working

I just had the same problem. It’s insanely poorly documented on ACF’s pages (as in, not there at all).

The core of your problem is, that you have spaces in your options-page names. And when you access it, using the ‘ID’, then you have to use the 'page_title', but where all spaces are replaced by underscores.

Please note, that it’s not all lowercase. It’s written exactly as you’ve done it, but just replacing the spaces with underscores.


Working example:

In functions.php:

if( function_exists( 'acf_add_options_page' ) ){

  acf_add_options_page( array(
    'page_title' => 'Some settings',
    'menu_title' => 'Some settings',
    'menu_slug'  => 'some_settings',
    'icon_url'   => 'dashicons-admin-generic',
    'capability' => 'edit_posts',
    'redirect'   => true,
    'position'   => '7.4',
  ) );

  acf_add_options_sub_page( array(
    'page_title'  => 'Acf page with spaces',
    'menu_title'  => 'Acf page with spaces',
    'parent_slug' => 'acf_page_with_spaces',
  ) );

} // endif

Where you want to access the fields (like page.php or single.php).

<?php if( have_rows( 'Acf_page_with_spaces', 'option' ) ): ?>
  <?php
  while( have_rows( 'Acf_page_with_spaces', 'option' ) ):
  the_row();
    $foo = get_sub_field( 'foo' );
    $bar = get_sub_field( 'bar' );
    ?>

    <h1><?php echo $foo; ?></h1>
    <p><?php echo $bar; ?></p>

  <?php endwhile; ?>
<?php endif; ?>

There’s something about this, that doesn’t make any sense, – but that’s how it works.


Extra note

This snippet helped me debugging this issue:

<?php
echo '<pre>';
print_r(acf_get_options_pages());
echo '</pre>';
?>

It prints all the registered options-pages.

Leave a Comment