Support

Account

Home Forums Front-end Issues acf_form, acf/save_post and acf/pre_save_posts

Helping

acf_form, acf/save_post and acf/pre_save_posts

  • Using acf_form() to create and update custom post types with custom taxonomies. I use four acf_fields to select the taxonomies, and then use acf/save_post to update them. It works when I update the posts but doesn’t work with new posts.

    I thought that maybe I was triggering before ACF saves the field values, but even with priority 999 get_field returns false.

    acf_form code for new posts:

    
    $gids = array();
    $groups = get_posts('post_type=acf&posts_per_page=-1');
    if ($groups){
    	foreach ($groups as $group){
    		$post_custom = get_post_custom($group->ID);
    		$rules = unserialize($post_custom['rule'][0]);
    		if ($rules['param'] == 'post_type' && $rules['value'] == 'bem-cultural'){
    			$gids[] = $group->ID;
    		}
    	}
    }
    acf_form(array(
    	'post_id'	=> 'new',
    	'field_groups'	=> $gids,
    	'submit_value'	=> 'Enviar'
    ));  
    

    functions.php

    //savetaxonomies
    function brg_save_taxonomies( $post_id ) {
        //get_fields 
        $cats = array();
        $cids = array();
        $cats[] = get_field('categoria'); //returns false with new posts. works on updates
        $cats[] = get_field('subcategoria');  //returns false with new posts. works on updates
        $cids[] = get_field('cidade'); //returns false with new posts. works on updates
        $cids[] = get_field('regiĆ£o'); //returns false with new posts. works on updates
        $cat_ids = array_map( 'intval', $cats ); 
        $cat_ids = array_unique( $cat_ids ); 
        
        $term_taxonomy_ids = wp_set_object_terms( $post_id, $cat_ids, 'categoria-bem-cultural' );
    
    	$cid_ids = array_map( 'intval', $cids );
    	$cid_ids = array_unique( $cid_ids ); 
    
    	$term_taxonomy_ids = wp_set_object_terms( $post_id, $cid_ids, 'cidade' );
    
    }
    
    // priority 20 to run after ACF saves data
    add_action('acf/save_post', 'brg_save_taxonomies', 20);
    
    //new posts
    function brg_pre_save_post( $post_id ) {
        // check if this is to be a new post
        if( $post_id != 'new' )
        {
            return $post_id;
        }
        //gets custom title
        $title = '';
        foreach ($_POST['fields'] as $key => $value){
    	$fobj = get_field_object($key);
    	if ($fobj['label'] == 'Nome do Bem'){
    		$title = $value;
    	}
        }
        // Create a new post
        $post = array(
            'post_status'  => 'publish',
            'post_title'  => $title,
            'post_type'  => 'bem-cultural',
        );
    
        // insert the post
        $post_id = wp_insert_post( $post );
        // update $_POST['return']
        $_POST['return'] = home_url() . '?p=' . $post_id;
    
        // return the new ID
        return $post_id;
    }
    
    add_filter('acf/pre_save_post' , 'brg_pre_save_post' );

    If I save the taxonomies inside pre_save_post it works, but the acf/save_post wipe them out. As a workaround, I’m checking if the post is old or new before updating in acf/save_post, but I would like to avoid using this patch.

  • Interesting problem. I don’t see anything in your code that would cause it.

    What are the field settings. Do you have “Load & Save” terms set for any of them?

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

The topic ‘acf_form, acf/save_post and acf/pre_save_posts’ is closed to new replies.