Support

Account

Home Forums ACF PRO ACF Form – form taxonomy check by default

Solved

ACF Form – form taxonomy check by default

  • Hello,

    I got a front end thanks to ACF pro plugin. In a field of a form I set “Field Type : Taxonomy” and by default it selects a specific category (I don’t know why). I wish to change it. So I have add this code (Without success) :

    add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
    function tsm_do_create_post_form() {
    $new_post = array(
    'post_id'            => 'new', // Create a new post
    'field_groups'       => array('group_54dfc93792c38', 'group_56afa46678eb3', 'group_56afa46adc147', 'group_569a815405075', 'group_56ab8bb40edcc', 'group_56bdf6aa906e1'), // Create post field group ID(s)
    'form'               => true,
    'return'             => '%post_url%', // Redirect to new post url
    'html_before_fields' => '',
    'html_after_fields'  => '',
    'submit_value'       => 'Poster',
    'updated_message'    => 'Saved!'
    );
    acf_form( $new_post );
    
    // Catégorie
    add_filter( 'acf/load_value/key=field_54dfccb977a11', 'vivi_load_post_category', 10, 3 );
    }
    
    function vivi_load_post_category( $value, $post_id, $field ) {
    $term_ids = array(71);
    return $term_ids;
    }

    Do you have a solution to select another category by default ?

  • try the load field filter instead

    
    add_filter('acf/load_field/key=field_54dfccb977a11', 'vivi_load_post_category');
    function vivi_load_post_category($field) {
      $field['default_value'] = array(71);
      return $field;
    }
    
  • Thank you , unfortunately it is still not functional

    add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
    function tsm_do_create_post_form() {
    	// Bail if not logged in or able to post
    	if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
    		echo do_shortcode( '[wppb-login] ' );
    		return;
    	}
    	$new_post = array(
    		'post_id'            => 'new', // Create a new post
    		'field_groups'       => array('group_54dfc93792c38', 'group_56afa46678eb3', 'group_56afa46adc147', 'group_569a815405075', 'group_56ab8bb40edcc', 'group_56bdf6aa906e1'), // Create post field group ID(s)
    		'form'               => true,
    		'return'             => '%post_url%', // Redirect to new post url
    		'html_before_fields' => '',
    		'html_after_fields'  => '',
    		'submit_value'       => 'Poster',
    		'updated_message'    => 'Saved!'
    	);
    	acf_form( $new_post );
    	
    	// Catégorie
    	add_filter('acf/load_field/key=field_54dfccb977a11', 'vivi_load_post_category');
    }
    
    function vivi_load_post_category($field) {
      $field['default_value'] = array(72);
      return $field;
    }
  • This

    
    add_filter('acf/load_field/key=field_54dfccb977a11', 'vivi_load_post_category');
    

    and this

    
    function vivi_load_post_category($field) {
      $field['default_value'] = array(72);
      return $field;
    }
    

    need to go into your functions.php file. It needs to be run when ACF loads fields and placing them where you have that will not happen.

  • Yes ! It wad the problem since the begining. At final I change the code in

    add_action( 'get_header', 'tsm_do_logged_in_author_checks' );
    function tsm_do_logged_in_author_checks() {
    
    	// Bail if not logged in or able to post
    	if ( ! ( is_user_logged_in()|| current_user_can('publish_posts') ) ) {
    		echo do_shortcode( '[wppb-login] ' );
    		return;
    	}
    
    	// Add required acf_form_head() function to head of page
    	add_action( 'get_header', 'tsm_do_acf_form_head', 1 );
    	
    	//Deregister the admin styles outputted when using acf_form
    	add_action( 'wp_print_styles', 'tsm_deregister_admin_styles', 999 );
    	
    	// Add the acf_form
    	add_action( 'genesis_entry_content', 'tsm_do_create_post_form' );
    	
    	// Catégorie
    	add_filter( 'acf/load_value/key=field_54dfccb977a11', 'vivi_load_post_category');
    }

    and this

    // Load défault categorie
    function vivi_load_post_category($field) {
    
    	// get Impro & Paris
    	$term_ids = array(28,72);
    
    	// return
    	return $term_ids;
    }

    It’s working perfectly.
    Thanks a lot John.

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

You must be logged in to reply to this topic.