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

I’m using the Advanced Custom Fields plugin and I have created some options pages with:

if (function_exists('acf_add_options_page')) {
    website_settings_init();
}

function website_settings_init() {
    $website_settings = acf_add_options_page(array(
        'page_title' => 'Website Settings',
        'menu_title' => 'Website Settings',
        'menu-slug' => 'website-settings',
        'post_id' => 'website-settings',
        'redirect' => false
    ));

    $form_settings = acf_add_options_sub_page(array(
        'page_title' => 'Form Settings',
        'menu_title' => 'Form Settings',
        'menu-slug' => 'form-settings',
        'post_id' => 'form-settings',
        'parent_slug' => $website_settings['menu_slug']
    ));

    $contact_details = acf_add_options_sub_page(array(
        'page_title' => 'Contact Details',
        'menu_title' => 'Contact Details',
        'menu-slug' => 'contact-details',
        'post_id' => 'contact-details',
        'parent_slug' => $website_settings['menu_slug']
    ));
}

I have a field group that is visible if Options Page = Form Settings.

One of these fields is ‘thank_you_page’ which returns a Page ID.

I read https://www.advancedcustomfields.com/resources/get-values-from-an-options-page/ which describes how you can get values from an options page field using get_field('field_name', 'option');

I have tried:

get_field('thank_you_page', 'option');

This returns null. I’ve tried both 'option' and 'options'. However, if I try:

get_field('thank_you_page', 'form-settings');

I get the saved Page ID. What am I misunderstanding/doing wrong? Is it because it is an options sub page?

I’m using WordPress 4.9.8 and ACF Pro 5.7.7

Thanks

1 Answer
1

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