Support

Account

Home Forums General Issues Empty previous entered field-data on save?

Solved

Empty previous entered field-data on save?

  • I have a custom post type (for information boxes on the homepage). I give the customer the possibility to choose between 4 layouts (conditions):

    1: header + content with colored border
    2: header + content with background color (ACF color picker field)
    3: header + content with background image (ACF image field)
    4: a google map (ACF google map field)

    This is how it looks like on the custom post type page:
    custom post type fields

    The problem is: When someone first has chosen e.g. a background-color and saved the post he has to empty the color field before he can choose another option (e. g. background-image), otherwise the new option will be ignored. Is there a possibility to delete entrys of other fields on post saving?

    This is my code:

    <?php 
    	// WP_Query arguments
    	$args = array (
    		'post_type' => array( 'thema' ),
    	);
    
    	// The Query
    	$query = new WP_Query( $args );
    
    	// The Loop
    	if ( $query->have_posts() ) {
    		$elements = [];
    		while ( $query->have_posts() ) {
    			$query->the_post(); ?>
    
    			<?php ob_start(); ?>
    
    				<?php if(get_field("box_hintergrundfarbe")): ?>
    				<!-- box with background color -->
    				<article <?php post_class('box cf sameheight-item') ?> style="background-color:<?php the_field("box_hintergrundfarbe"); ?>;">
    					<div class="box-inner cf">
    						<h2><?php the_title(); ?></h2>
    						<?php the_content();  ?>
    					</div>
    				</article>
    
    				
    				<?php elseif(get_field("box_hintergrundbild")): ?>
    				<!-- box with background image -->
    				<article <?php post_class('box cf sameheight-item') ?> style="background-image: url(<?php the_field("box_hintergrundbild"); ?>)">
    					<div class="box-inner light cf">
    						<h2><?php the_title(); ?></h2>
    						<?php the_content(); ?>
    					</div>
    				</article>
    
    				
    				<?php elseif(get_field("location")): ?>
    				<!-- box with google map -->
    				<article <?php post_class('box cf sameheight-item') ?>>
    					<div class="cf">
    						<?php 
    							$location = get_field("location");
    							if( !empty($location) ):
    						?>
    						<div class="acf-map">
    							<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
    						</div>
    						<?php endif; ?>
    					</div>
    				</article>
    
    				<?php else: ?>
    				<!-- regular box with border -->
    				<article <?php post_class('box cf sameheight-item') ?>>
    					<div class="box-inner cf" style="border: 3px <?php the_field('farbe_b','option'); ?> solid; border-bottom: none;">
    						<h2><?php the_title(); ?></h2>
    						<?php the_content();  ?>
    					</div>
    				</article>
    
    			<?php endif; ?>
    
    			<?php $content = ob_get_clean(); ?>
    
    			<?php
    				$element = $content;
    				$elements[] = $element;
    		}
    
    		if(sizeof($elements)>0) {
    			$size = ceil(12 / sizeof($elements));
    			if($columns<2) $columns = 2;
    
    			foreach($elements AS $element) {
    				echo '<div class="col-md-'.$size.'">'.$element.'</div>';
    			}
    		}
    
    	} else {
    		// no posts found
    	}
    
    	// Restore original Post Data
    	wp_reset_postdata();
    ?>
  • I actually just answered a similar question. http://support.advancedcustomfields.com/forums/topic/delete-repeater-row-not-staying-deleted-with-onditional-logic/

    In you case, rather than

    if (get_field('content_field'))

    You should do

    $selection = get_field('radio_field');
    
    if ($selections == 'choice 1') {
       // output code for choice 1
    }
    
    if ($selections == 'choice 2') {
       // output code for choice 2
    }
    
    

    Or you could combine them into an if elseif else statement. Of even better yet I would probably choose a switch statement.

  • Thanks, i found the “acfsave_post” page, too but did not understand a thing. I will try to empty the tables on save, i don’t want to put ghost data in the DB …

  • have the filter run after ACF (priority 20)

    Then you get use get_field to get the radio field and update_field if you want to clear it but leave the DB entries.

    If you want to completely delete the db entry you’ll need to use delete_post_meta() WP function and you will need to delete 2 values.

    'your_field_name'

    AND

    '_your_field_name'

    If you are doing this for repeater fields or other nested fields you should look at the database to see how ACF names these types of fields.

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

The topic ‘Empty previous entered field-data on save?’ is closed to new replies.