Support

Account

Home Forums ACF PRO ACF_Form – Default taxonomy via 'tax_input' without increasing permissions

Solved

ACF_Form – Default taxonomy via 'tax_input' without increasing permissions

  • Hi there,

    I have a super simple form where I only want to make a post where I set the title, taxonomy, and author. Taxonomy and author are default and I would like to set these programmatically instead of via the fields editor in the admin. I’ve tried doing that by adding values in the ‘new_post’ array bit of the acf_form() options, but this doesn’t appear to be working

    $options = array(
    	'post_id'			=> 'new_post',
    	'post_title'			=> true,
    	'new_post'			=> array(
    		'post_type'		=> 'my-cpt',
    		'post_status'		=> 'publish',
    		//'post_author'		=> 'default-user',  // This doesn't work
    		//'my-taxonomy' 	=> 'my-taxo-slug',  // Nor does this work
    	),
    	'field_groups'			=> array(
    		1234
    	),
    	'submit_value' 			=> __("Add CPT", 'acf'),
    	'updated_message' 		=> __("CPT Added", 'acf'),
    	'uploader'			=> 'wp'
    );
    acf_form($options);

    So, could anyone let me know how I might be able to set the author and taxonomy defaults for acf_form() to create a custom post type?

    Thanks in advance!

  • Ok, I’ve figured out the author bit, because I needed to use the ID. I’m still stuck on the taxonomy

    
    $options = array(
    	'post_id'			=> 'new_post',
    	'post_title'			=> true,
    	'new_post'			=> array(
    		'post_type'		=> 'my-cpt',
    		'post_status'		=> 'publish',
    		'post_author'		=> $user_id  // Where $user_id is a number
    		'tax_input'		=> array('my_taxonomy' => $taxo_id), // Not working
    	),
    	'field_groups'			=> array(
    		1234
    	),
    	'submit_value' 			=> __("Add CPT", 'acf'),
    	'updated_message' 		=> __("CPT Added", 'acf'),
    	'uploader'			=> 'wp'
    );
    acf_form($options);
    

    Regarding the taxonomy, I’ve read this thread:

    acf_form, new_post, tax_input requires to be logged in

    – and –

    Using ‘tax_input’ with wp_insert_post() and nothing happens

    …so it seems that the user needs permission to assign taxonomies – OR – I need to use wp_set_object_terms() using acf/save_post. I’ve increased permissions, which appears to have worked, but I am curious if there’s a way to do this via wp_set_object_terms() so I don’t have to increase permissions.

    Does anyone have any ideas?

    Thanks in advance!

  • Hi @ryandorn

    As you have figured out, I believe you can use the acf/save_post hook to add the terms. Have you tried it? Do you face any difficulties with it?

    Thanks 🙂

  • Hi James,

    Thanks for your reply. Unfortunately I’ve not yet tried theacf/save_post hook on this. I’m not entirely sure how to accomplish this and generally have difficulty with understanding this hook.

    Best,

    Ryan

  • Hi @ryandorn

    The acf/save_post hook will be executed everytime a post is saved. If you add an action in that hook, it will be executed after the post is saved. If you don’t know the concept of WordPress’ hook, here’s an article that explains it: http://wpcandy.com/teaches/how-to-use-wordpress-hooks/.

    You can execute the wp_set_object_terms() function in that hook, so the terms will be added automatically when you saved a post. It should be something like this:

    function my_acf_save_post( $post_id ) {
        
        // get the post object
        $the_post = get_post($post_id);
        
        // check if custom post type
        if( $the_post->post_type == 'my-cpt' ) {
            // add the term. Change 'true' to 'false' if you want to override it
            wp_set_object_terms( $post_id, 'my-term-slug', 'my-taxonomy-slug', true );
        }
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    I hope this makes sense 🙂

  • Wanted to give some help. I was stuck on this part for a while to. When you use acf_form() you NEED acf_form_header() FOR SURE if the person is not logged in (unsure about admins logged in)… BUT the save_post method is good. It ISN’T capabilities that is messing it up. I tested to be sure by making the author in acf_form() be the admin (all capabilities) and that didn’t solve the problem. So the save post method is the best BUT then it is hard to get OTHER variables outside the post you created (like from the page you used acf_form() on etc):

    
    $htmlAfter = '<input type="hidden" name="tax_name" id="tax_name" value="ENTER_MAIN_TAXONOMY_SLUG_HERE">';
    	$htmlAfter .= '<input type="hidden" name="tax_slug" id="tax_slug" value="ENTER_INDIVIDUAL_TAXONOMY_SLUG_HERE">';

    Then include it in the acf_form() via the parameter “html_after_fields”:

    'html_after_fields' => $htmlAfter

    THEN, the save_post function:

    function my_acf_save_post( $post_id ) {
        
       if(isset($_POST['tax_name']) && isset($_POST['tax_slug'])) {
    	 wp_set_object_terms( $post_id, $_POST['tax_slug'], $_POST['tax_name'], true );
    	}
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);
    

    You could enter the values for taxonomy and slug manually or set them as variables conditionally based on whatever you want, where ever you include acf_form() …

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

The topic ‘ACF_Form – Default taxonomy via 'tax_input' without increasing permissions’ is closed to new replies.