Support

Account

Home Forums ACF PRO Set Default Post meta_value(s) and taxonomy when adding post on front end

Solving

Set Default Post meta_value(s) and taxonomy when adding post on front end

  • Hi there!

    I have an acf_form() that I’m using to create a custom post type from the front end. I want to set various meta_keys and taxonomies by default. I’m still trying to wrap my head around acf/save_post, so thanks in advance for any help and understanding.

    What I was doing: using ACF fields that were hidden and readonly via jQuery with default values.

    Why I don’t want to use this anymore: doesn’t seem very secure / seems hackable

    What I would like to do:

    Run wp_set_object_terms AND wp_update_post with acf/save_post.

    What I have so far, unsurprisingly, is not working…hopefully with this, though, someone will at least understand where I’m trying to go with this.

    function my_default_fields($post_id) {
    
    	// Default Meta Key Array
    	$default_meta_values = array(
    		'_meta_val_1'	=> 'derp',
    		'_meta_val_2'	=> 'herp'
    	);
    	
    	// Set Default Meta Keys Loop
    	foreach($default_meta_values as $meta_key=>$meta_value) {
    	   update_post_meta($post_id, $meta_key, $meta_value);
    	}
    	
    	wp_set_object_terms( $post_id, 'taxo_value', 'my_taxo' );
    
    }
    
    add_action('acf/save_post', 'my_default_fields', 20);
    
    // Front-end Form
    
    $options = array(
    	'post_id'			=> 'new_post',
    	'post_title'		=> true,
    	'new_post'			=> array(
    		'post_type'		=> 'my_custom_post_type',
    		'post_status'	=> 'publish',
    	),
    	'field_groups'		=> array(
    		1234
    	),
    	'return'			=> home_url('my-uri'),
    	'submit_value' 		=> __("Create post", 'acf'),
    	'updated_message' 	=> __("Post created", 'acf'),
    	'uploader'			 => 'wp'
    );
    acf_form($options);
  • Well, it turns out that the above code does work. I think that I had some conflicts.

    How can I target only one, specific form via: ‘acf/save_post’?

  • Hi @ryandorn

    You can always create a hidden field like you did before to determine if the data is posted from certain forms. In the acf/save_post hook, you can check the $_POST data for the hidden fields.

    I hope this makes sense 🙂

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

The topic ‘Set Default Post meta_value(s) and taxonomy when adding post on front end’ is closed to new replies.