Support

Account

Home Forums Front-end Issues Radio field conditional in afc_form?

Helping

Radio field conditional in afc_form?

  • Hi

    I have create a post frontend with acf_form. How to select conditional radio field?
    This is my code, but not correct. I using code if(get_field('chon') == 'a') {}

    Field name radio: chon
    Choice: a : Image
    b : Video

    createpost.php

    <?php acf_form_head(); ?>
    <?php 
    /*
    Template Name: dangbai
    */
    get_header(); 
    ?>
    <div id="primary" class="content-area home">
    	<main id="main" class="site-main" role="main">
    	
    	<?php while ( have_posts() ) : the_post(); ?>
    		<?php 
    		acf_form(array(
    				'post_id'		  => 'new_post',
    				'submit_value'	  => 'Đăng bài',
    				'field_groups'	  => array( 305 ),
    				'updated_message' => 'Đã đăng bài'
    			));
    		?>
    	<?php endwhile; ?>	
    
    	</main><!-- #main -->
    </div><!-- #primary -->
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>

    Function.php

    
    function my_pre_save_post( $post_id ) {
    	
    	// bail early if not a new post
    	if( $post_id !== 'new_post' ) {	 
    		return $post_id;	
    	}
    	
    	// vars
    	//$title = $_POST['fields']['field_5558a08e10eed'];
    	//$content = $_POST['fields']['field_555ddb4112814'];
    	
    	//If
    	if(get_field('chon') == 'a') {
    		$title = $_POST['fields']['field_5558a08e10eed'];
    		$content = $_POST['fields']['field_555ddb4112814'];
    	}
    	else if (get_field('chon') == 'b') {
    		$title = $_POST['fields']['field_555efea0f3e01'];
    		$content = $_POST['fields']['field_555efed7f3e02'];
    	}
    
    	// Create a new post
    	$post = array(
    		'post_status'	=> 'publish',
    		'post_type'		=> 'post',
    		'post_title'	=> $title,
    		'post_content'	=> $content
    	);	
    	
    	// insert the post
    	$post_id = wp_insert_post( $post ); 
    	
    	// update $_POST['return']
        $_POST['return'] = add_query_arg( array('post_id' => $post_id), $_POST['return'] );
    	
    	// return the new ID
    	return $post_id;
    }
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

    Thanks!

  • get_field(‘chon’) is not going to work here.

    
    
    if(get_field('chon') == 'a') {
        $title = $_POST['fields']['field_5558a08e10eed'];
        $content = $_POST['fields']['field_555ddb4112814'];
    }
    else if (get_field('chon') == 'b') {
        $title = $_POST['fields']['field_555efea0f3e01'];
        $content = $_POST['fields']['field_555efed7f3e02'];
    }
    

    Your function is called before the field is saved, so it will have no value.

    Your accessing the value for the title and content the correct way, you” need to access the value of ‘chon’ the same way.

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

You must be logged in to reply to this topic.