admin管理员组

文章数量:1122846

How can I ensure that the data saved by an Advanced Custom Fields Relationship field, when on an Options page for a Post type, is separate?

Detail below:

I have an ACF field group, “Features”, containing a Relationship field.

It is intended to allow me to pre-set some featured posts for a couple of custom post types – “Reports” and “Articles”.

For those post types, in the same code that registers each post type, I have created an Options sub-page like this…

// Support showcase Features for this post type, via ACF

if( function_exists('acf_add_options_page') ) {

    // add sub page
    acf_add_options_sub_page(array(
        'page_title'    => 'Articles Features',
        'menu_title'    => 'Articles Features',
    'menu_slug'   => 'articles-features',
    'capability'  => 'edit_posts',
        'parent_slug'   => 'edit.php?post_type=article',
    'position'    => false,
    'icon_url'    => false
    ));

}

The same goes for “Reports”.

And then I apply the “Features” field group to both Options sub-pages – “Articles Features” and “Reports Features”.

The problem is… the data is the same. That is, the six Relationship posts set for “Reports Features” also show up in the Options sub-page for “Articles Features”. Changing one Relationship field overwrites the other.

How can I ensure that these two things are separate, so that I can save the features distinctly per post type, without recreating the field group?

How can I ensure that the data saved by an Advanced Custom Fields Relationship field, when on an Options page for a Post type, is separate?

Detail below:

I have an ACF field group, “Features”, containing a Relationship field.

It is intended to allow me to pre-set some featured posts for a couple of custom post types – “Reports” and “Articles”.

For those post types, in the same code that registers each post type, I have created an Options sub-page like this…

// Support showcase Features for this post type, via ACF

if( function_exists('acf_add_options_page') ) {

    // add sub page
    acf_add_options_sub_page(array(
        'page_title'    => 'Articles Features',
        'menu_title'    => 'Articles Features',
    'menu_slug'   => 'articles-features',
    'capability'  => 'edit_posts',
        'parent_slug'   => 'edit.php?post_type=article',
    'position'    => false,
    'icon_url'    => false
    ));

}

The same goes for “Reports”.

And then I apply the “Features” field group to both Options sub-pages – “Articles Features” and “Reports Features”.

The problem is… the data is the same. That is, the six Relationship posts set for “Reports Features” also show up in the Options sub-page for “Articles Features”. Changing one Relationship field overwrites the other.

How can I ensure that these two things are separate, so that I can save the features distinctly per post type, without recreating the field group?

Share Improve this question asked Apr 13, 2019 at 8:09 Robert AndrewsRobert Andrews 98819 silver badges42 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

The method to use is...

In the acf_add_options_sub_page declaration, add 'post_id' => 'article' as a parameter. (post_id)

This is how the field data, as entered on the Options page corresponding to my post type, will be saved.

On the display end, display using $featured_posts = get_field('featured_posts', 'article');

'article' and 'report' are both used.

In my case, at the display end, I use $queried_object->name to dynamically infer either 'article' or 'report' post types.

Credit hube2

本文标签: Same ACF Relationship field for multiple Post Options subpages