Support

Account

Home Forums Backend Issues (wp-admin) acf_add_local_field not saving in a custom post type

Helping

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;
    }
  • Why its taking so long to read this post.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.