Home › Forums › Backend Issues (wp-admin) › acf_add_local_field not saving in a custom post type
So I have generated a repeater field in my custom post type which is products but when I tried to save it and when the page refreshed all the added data is gone.
Here is my code:
function wpt_add_product_metaboxes() {
global $post;
$unique_terms = array();
$postID = $post->ID;
$have_terms = get_the_terms($postID, 'product_attributes');
if($have_terms):
$parent_terms = wp_get_post_terms( $postID,'product_attributes', array( 'parent' => 0, 'orderby' => 'id', 'order' => 'DESC', 'hide_empty' => false ) );
#print("<pre>".print_r($parent_terms,true)."</pre>");
$ctr = 0;
foreach ($parent_terms as $p_term):
$custom_taxnomies_child = wp_get_post_terms($postID, 'product_attributes', array( 'parent' => 66, 'orderby' => 'slug', 'hide_empty' => false ));
foreach($custom_taxnomies_child as $cterm){
if( ! in_array( $cterm->term_id, $unique_terms ) ){
array_push( $unique_terms, $cterm->term_id );
$ctr++;
acf_add_local_field( array (
'key' => 'field_tab_size_'.$ctr,
'label' => $cterm->name,
'name' => 'store_sizes_'.$ctr,
'type' => 'tab',
'parent' => 'field_5bd14c9349930',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
));
/**
* Initial Repeater Field
*
*/
acf_add_local_field( array (
'key' => 'field_product_sizes_prices'.$ctr,
'label' => 'Product Sizes and Prices',
'name' => 'product_sizes_'.$ctr,
'type' => 'repeater',
'parent' => 'field_5bd14c9349930',
'instructions' => '',
'required' => 0,
'conditional_logic' => 0,
'wrapper' => array (
'width' => '',
'class' => '',
'id' => '',
),
'collapsed' => '',
'min' => '',
'max' => '',
'layout' => 'table',
'button_label' => 'Add Row'
));
/**
* Add Start Date Subfield
*
*/
acf_add_local_field( array (
'key' => 'field_total_products_'.$ctr,
'label' => 'Total Products',
'name' => 'total_products'.$ctr,
'parent' => 'field_product_sizes_prices'.$ctr, // key of parent repeater
'type' => 'text',
'instructions' => '',
));
/**
* Add End Date Subfield
* */
acf_add_local_field( array (
'key' => 'field_total_prices_'.$ctr,
'label' => 'Total Prices',
'name' => 'total_prices',
'parent' => 'field_product_sizes_prices'.$ctr,
'type' => 'text',
));
}
endforeach;
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βre excited to announce we've released Composer support for installing ACF PRO.
— Advanced Custom Fields (@wp_acf) January 31, 2023
π #ComposerPHP fans rejoice!
β¨ Please see the release post for all the details and full instructions. https://t.co/ebEfp61nlR
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.