最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ACF for custom post type archive pages: which hook to use?

programmeradmin1浏览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)

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)

Share Improve this question edited Oct 9, 2019 at 13:19 Dan Inactive asked Oct 9, 2019 at 10:57 Dan InactiveDan Inactive 1431 silver badge9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论