0

I'm trying to register ACF options pages for each custom post type defined in the theme:

$post_types = get_post_types(
    array(
        'public'   => true,
        '_builtin' => false
    ),
    'objects'
);

foreach ($post_types as $post_type) {
    $post_type_slug = $post_type->name;
    $options_title = $post_type->labels->name . ": Archive Options";
    acf_add_options_page(array(
        'page_title' => $options_title,
        'menu_title' => $options_title,
        'parent_slug'   => "edit.php?post_type={$post_type_slug}",
        'menu_slug' => "{$post_type_slug}-archive-options",
        'capability' => 'edit_posts',
        'redirect' => false
    ));
}

This is called on the acf/init hook (as recommended by the docs), while the custom post types are registered on WordPress init hook. (I should also probably mention that both actions are hooked as part of an after_setup_theme action.)

However, get_post_types() returns an empty array. Is this a sequence mismatch between the hooks? (i.e. posts are registered only after the acf/init hook)

1

Is this a sequence mismatch between the hooks?

I would argue so, yes. acf/init gets executed by class ACF's init method, all in the root file acf.php - this method is hooked to WordPress' init with a priority of 5 like so

add_action( 'init', array($this, 'init'), 5 );

And you probably add your CPTs with a higher priority, this means acf/init will be executed before the registering of the custom post types.

To solve this, I would hook into init with a even higher priority (smaller than 5) to register the CPTs.

  • 1
    Thank you, @kero! That was exactly it. – Dan Burzo Oct 9 at 13:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.