Home › Forums › Add-ons › Repeater Field › Custom Post Type Generation Using ACF Repeater Fields ONLY
Hello,
I’m trying to implement a system wherein a user can create their own custom post types on the backend of their WordPress website purely using ACF and without relying on additional plugins such as CPT UI.
I have written the below code but can’t seem to get it working and I can’t figure out why:
Do you know if this functionality is possible purely using ACF, or if I’m missing something obvious?
I’m fairly new to PHP and ACF, but have managed to implement quite a lot of custom functionality using ACF alone so far and am loving everything the plugin has to offer!
If you could advise me on the correct route to take here it would be much appreciated!
All of your code starting at line 5 and going to line 52 needs to be inside of the while loop after the_row()
is called. get_sub_field()
will not return any values before then. Event if it did, with the code outside of the loop you would be assigning the same values (attempting to create the same post type) on for every row of the repeater.
Thank you for the reply! I have now amended the code as per your suggestion but it still doesn’t seem to be registering my CPTs:
Do you have any other ideas about why this code might not be running?
If it helps add any context, this code is utilised in a separate, dedicated PHP file which has been called through as ‘require’ in my functions.php file.
I’ve done some A/B testing, and it looks as though none of the custom variables I’ve defined in my example code above are pulling through at all.
For example, changing the output of the $cpt_slug variable to output a simple “post-type-name” string (instead of pulling through any ACF field data) results in the CPT not registering, but typing the exact same “post-type-name” string into the register_post_type action as shown below, does register the CPT (albeit without any of the custom labels I”m trying to get working):
register_post_type( "post-type-name", $args );
Additionally, the if / else statements defined for the $labels arrays aren’t working properly either.
I know this because changing all of the $args lines to be written in their standard format without any variable or ACF field calls at all, results in the default else: variations pulling through instead.
Note: None of the above information has solved my issue, but hopefully it will help clarify which parts of my code just aren’t working from the offset!
This has now been solved:
if( have_rows('custom_post_types', 'option') ):
while( have_rows('custom_post_types', 'option') ) : the_row();
// ACF Custom Label Declarations
$cpt_name_plural = esc_html__( '"' . get_sub_field('cpt_name_plural') . '"' );
$cpt_name_single = esc_html__( '"' . get_sub_field('cpt_name_single') . '"' );
$cpt_label_add_new = esc_html__( '"' . get_sub_field('cpt_label_add_new') . '"' );
$cpt_label_add_new_item = esc_html__( '"' . get_sub_field('cpt_label_add_new_item') . '"' );
$cpt_label_edit_item = esc_html__( '"' . get_sub_field('cpt_label_edit_item') . '"' );
$cpt_label_new_item = esc_html__( '"' . get_sub_field('cpt_label_new_item') . '"' );
$cpt_label_view_item = esc_html__( '"' . get_sub_field('cpt_label_view_item') . '"' );
$cpt_label_search_items = esc_html__( '"' . get_sub_field('cpt_label_search_items') . '"' );
$cpt_label_not_found = esc_html__( '"' . get_sub_field('cpt_label_not_found') . '"' );
$cpt_label_not_found_in_trash = esc_html__( '"' . get_sub_field('cpt_label_not_found_in_trash') . '"' );
$cpt_label_parent_item_colon = esc_html__( '"' . get_sub_field('cpt_label_parent_item_colon') . '"' );
$cpt_label_all_items = esc_html__( '"' . get_sub_field('cpt_label_all_items') . '"' );
$cpt_label_archives = esc_html__( '"' .
get_sub_field('cpt_label_archives') . '"' );
$cpt_label_attributes = esc_html__( '"' . get_sub_field('cpt_label_attributes') . '"' );
$cpt_label_insert_into_item = esc_html__( '"' . get_sub_field('cpt_label_insert_into_item') . '"' );
$cpt_label_uploaded_to_this_item = esc_html__( '"' . get_sub_field('cpt_label_uploaded_to_this_item') . '"' );
$cpt_label_featured_image = esc_html__( '"' . get_sub_field('cpt_label_featured_image') . '"' );
$cpt_label_set_featured_image = esc_html__( '"' . get_sub_field('cpt_label_set_featured_image') . '"' );
$cpt_label_remove_featured_image = esc_html__( '"' . get_sub_field('cpt_label_remove_featured_image') . '"' );
$cpt_label_use_featured_image = esc_html__( '"' . get_sub_field('cpt_label_use_featured_image') . '"' );
$cpt_label_menu_name = esc_html__( '"' . get_sub_field('cpt_label_menu_name') . '"' );
$cpt_label_filter_items_list = esc_html__( '"' . get_sub_field('cpt_label_filter_items_list') . '"' );
$cpt_label_items_list_navigation = esc_html__( '"' . get_sub_field('cpt_label_items_list_navigation') . '"' );
$cpt_label_items_list = esc_html__( '"' . get_sub_field('cpt_label_items_list') . '"' );
$cpt_label_name_admin_bar = esc_html__( '"' . get_sub_field('cpt_label_name_admin_bar') . '"' );
// Register Custom Post Type
$cpt_name = get_sub_field('cpt_name');
$cpt_args = array(
'label' => $cpt_name,
'description' => '',
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_rest' => false,
'rest_base' => '',
'has_archive' => true,
'show_in_menu' => true,
'exclude_from_search' => false,
'capability_type' => 'post',
'map_meta_cap' => true,
'hierarchical' => false,
'rewrite' => array( 'slug' => $cpt_name, 'with_front' => true ),
'query_var' => true,
'menu_position' => 5,
'menu_icon' => 'dashicons-admin-post',
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'custom-fields', 'comments' ),
'taxonomies' => array( 'category', 'post_tag' ),
'labels' => array(
'name' => $cpt_name_plural,
'singular_name' => $cpt_name_single,
'menu_name' => $cpt_label_menu_name,
'add_new' => $cpt_label_add_new,
'add_new_item' => $cpt_label_add_new_item,
'edit_item' => $cpt_label_edit_item,
'new_item' => $cpt_label_new_item,
'view_item' => $cpt_label_view_item,
'search_items' => $cpt_label_search_items,
'not_found' => $cpt_label_not_found,
'not_found_in_trash' => $cpt_label_not_found_in_trash,
'parent_item_colon' => $cpt_label_parent_item_colon,
'all_items' => $cpt_label_all_items,
'archives' => $cpt_label_archives,
'attributes' => $cpt_label_attributes,
'insert_into_item' => $cpt_label_insert_into_item,
'uploaded_to_this_item' => $cpt_label_uploaded_to_this_item,
'featured_image' => $cpt_label_featured_image,
'set_featured_image' => $cpt_label_set_featured_image,
'remove_featured_image' => $cpt_label_remove_featured_image,
'use_featured_image' => $cpt_label_use_featured_image,
'filter_items_list' => $cpt_label_filter_items_list,
'items_list_navigation' => $cpt_label_items_list_navigation,
'items_list' => $cpt_label_items_list,
'name_admin_bar' => $cpt_label_name_admin_bar
)
);
register_post_type( $cpt_name, $cpt_args );
endwhile;
endif;
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.