Home › Forums › Add-ons › Options Page › Same Option page for different CPT
Hello 🙂
I have different Custom Post Types (CPT) registerd and every of them needs the same set of options but of couse with different content.
As far as I could figuer out, it’s not easily possible with acf option pages, because all options are saved in wp_options database so as soon as the option fields from different cpt’s have the same name they gonne overwrtite each other. Is there any other possibility to have several option pages for cpt?
Right now I created an extra page for each cpt, where I could add the options to. I made this page the new archive page by adding a custom template, so it delivers the option fields and also the rest of the pages. The page itself is than excludet from cpt archives while having an offset in wp_query.
This approach is working so far, but it is far from beeing user friendly and handy.
Is there any way to launch acf option pages for each cpt in this szenario or is there any other idea to realize this in a slightly more sexy way?
Thanks and a beautiful day to everybody 🙂
You need to specify a different post ID. Instead of using the standard “options” you can use something like “my-cpt-options” and then you can use this to get the values for that options page. In all other ways it works the same as using “options”
To expand on that, when saving options ACF saves the values to the options table and the option_name value is prefixed with “options_”, for example “options_my_text_field”. When you specify a different post ID like “my_cpt_options” the same field will be saved with the name “my_cpt_options_my_text_field” making the two entries unique.
Thanks for bringing me on the right way 🙂
Now I can create a CPT including an options page with a Repeater. That’s super handy for me. Just the field group needs to be linked again from the acf settings to the new option page (is there any easy solution for automation?).
For anyone looking for something similar, here the code from the functions.php. No clue if it’s perfect, but it works well for me.
// CREATES CUSTOM POSTTYPE WITH OPTION PAGES
function instrument_manager() {
if( have_rows('instrument_manager', 'option') ):
// add specific Option Pages for each CPT
if( function_exists('acf_add_options_page') ) {
while( have_rows('instrument_manager', 'option') ) : the_row();
$post_type_title = get_sub_field('instrument_title', 'option'); //can be every title
$post_type_slug = get_sub_field('instrument_slug', 'option'); //no special chars, no caps
acf_add_options_sub_page(array(
'page_title' => 'Settings ' . $post_type_title,
'menu_title' => 'Settings ' . $post_type_title,
'parent_slug' => 'edit.php?post_type=' . $post_type_slug,
'post_id' => 'options_' . $post_type_slug,
));
endwhile;
}
// create Custom Post Types
function create_posttype() {
while( have_rows('instrument_manager', 'option') ) : the_row();
$post_type_title = get_sub_field('instrument_title', 'option');
$post_type_slug = get_sub_field('instrument_title', 'option');
$lables = array('name' => __( $post_type_title ), 'singular_name' => __( $post_type_title ));
$args = array(
'labels' => $lables,
'public' => true,
'publicly_queryable' => true,
'supports' => array( 'title', 'editor', 'page-attributes' ),
'taxonomies' => array( 'category', 'post_tag' ),
);
register_post_type( $post_type_slug , $args );
endwhile;
}
endif;
}
add_action('init', 'instrument_manager');
add_action( 'init', 'create_posttype' );
and to read out the settings in my archive.php I used this line:
$post_type = 'options_' . get_query_var('post_type');
the_field('field-name', $post_type) ?>
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.