Support

Account

Forum Replies Created

  • Hi @novelgroup

    Without creating an hidden field I have tried to get post_type value in acf/pre_save_post action but get_post_type($post_id) did not worked. So far I can get the value using $GLOBALS variable e.g

     if ( $GLOBALS["acf_form"]["new_post"]["post_type"] == "cpt_name" ) {
     do whatever 
    }
  • Hi @novelgroup

    Well I think so we can get the post_type from the acf_form array and use that to change the content for each post type but as to me simple way is to just add a hidden field with post type value and get that in acf/pre_save_post action. I just tried this and its working for me.

    First add the hidden field in acf_from and then get the value

    
    'html_after_fields' => '<div><input type="hidden" name="my_pt" value="u_talez"></div>',
    
    function acf_review_before_save_post($post_id) {
    
    	if (empty($_POST['acf']))
    		return;
    
    	//form 1 
    	$Post_type = $_POST['my_pt'];
    
    	if ( $Post_type == 'cpt' ) {
    		$_POST['acf']['_post_title'] = $_POST['acf']['field_577ba2a05bb33'];
    		$_POST['acf']['_post_content'] = $_POST['acf']['field_577ba2b153bb51'];
    	}
    
    	//form2
    	$Post_type = $_POST['my_pt2'];
    
    	if ( $Post_type == 'pt2' ) {
    		$_POST['acf']['_post_title'] = $_POST['acf']['field_577ba2a05bb33'];
    		$_POST['acf']['_post_content'] = $_POST['acf']['field_577ba2b153bb51'];
    	}	
    
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
  • As @Jonathan said above you just need hook acf/pre_save_post action to save custom title and content field into post title and content field. See sample code below

    Just replace custom field keys below with specific field keys from field group.

    
    /**
     *
     * Saving Custom fields into post fields acf_forms 
     *
     */
    
    function acf_review_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_577ba2a05bb54'];
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_577ba2b15bb55'];
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
Viewing 3 posts - 1 through 3 (of 3 total)