Support

Account

Home Forums Front-end Issues Selecting Category from Frontend Form

Solved

Selecting Category from Frontend Form

  • I have a form that creates new posts and works well for taking my title & content from the form and putting them into a new post. I would like to have category also selected through the form. I created a field with selections that match my categories, but am not sure how to have those added to the new post. If there is a way to use the standard WP categories directly on my form that would work, but if not how can I have my field of the categories pushed to the WP categories when the form makes the new post?

    I tried adding it to the new_post code but that did not work.

    'new_post'		=> array(
    			'post_type'		=> '',
    			'post_status'		=> 'publish',
    			'post_title'    => $_POST['acf']['field_558eb15c51fc7'], // Post Title ACF field key
    			'post_content'  => $_POST['acf']['field_558eb19c51fc8'], // Post Content ACF field key
    			'post_category' => $_POST['acf']['field_55d73c0ee19e4'], // Post Category ACF field key
    					),

    I also setup my category field in ACF to be the category ID for its value but this also does not seem to work

    Thanks for any assistance.

  • Hi,

    Are you using ACF PRO? (v5)

  • Yes I have ACFPro V5.

    Andy

  • Okay,

    Could you post the whole code you’re using to create the form?

    I’m not sure if you already are but you should use acf_form() function to create the form. That way you’ll get everything working right of the bat 🙂

    http://www.advancedcustomfields.com/resources/acf_form/

  • Function.php

    
    function acf_review_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    	$_POST['acf']['_post_title'] = $_POST['acf']['field_558eb15c51fc7'];
    	$_POST['acf']['_post_content'] = $_POST['acf']['field_558eb19c51fc8'];
    	$_POST['acf']['_post_category'] = $_POST['acf']['field_55d73c0ee19e4'];
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
    

    Template for page with form

    <?php acf_form(array(
     'post_id'	=> 'new_post',
     'new_post'	=> array(
       'post_type'	=> '',
       'post_status'=> 'publish',
       'post_title'  => $_POST['acf']['field_558eb15c51fc7'], // Post Title ACF field key
       'post_content'  => $_POST['acf']['field_558eb19c51fc8'], // Post Content ACF field key
       'post_category' => $_POST['acf']['field_55d73c0ee19e4'], // Post Category ACF field key
    ),
       'field_groups'   => array(317), // Create post field group ID(s)
       'form'               => true,
    //'return'             => '%post_url%', // Redirect to new post url
       'html_before_fields' => '',
       'html_after_fields'  => '',
       'submit_value'       => 'Submit Post',
       'updated_message'    => 'Saved!',
        'uploader' 			 => 'wp',
        'return' 		=> '/'
       )); ?>
  • Hi @amurray

    I’m not able to wrap my head around where you place the acf_form(); function.

    You’re using acfs internal $_POST values in the acf_form function but that would require that you’re at a page posted to by ACF already.
    How are people directed to the page template where you use this acf_form?

  • I have a page that has basically just this form on it. It is link directly from a menu button. If there are better ways to do this than I am open to suggestions. The pages works well for creating the posts and copying my title field & content field to the standard WP post fields.

    It could be that the _Post lines in the template are not actually working and it is the ones in the function.php that are actually doing the copying from the front-end form to the WP posts. I did initially have issues with these fields not populating & did some searching to find the code that is in my function.php section.

    Andy

  • Jonathan,

    Any further suggestions on making this work?

    Thanks.
    Andy

  • Hi Andy,

    Sorry I’ve been involved in some personal stuff the past few days 🙂

    To be honest I’ve not used the acf_form in this sense before.
    What seems strange to me is that you’re setting post_title, post_content and post_category by using $_POST values but if you’re sending the user to this form with just a button there wont be any $_POST variables like this.

    The way I’d do it is to just have a taxonomy field on the posts which are mapping the post to the terms (categories) and then add it to the acf_form by setting it in the field_group. Then for the title and content you have built in parameters in acf_form to let users type into those.

    Something like:

    
    <?php acf_form(array(
    	'post_id'	=> 'new_post',
    	'new_post'	=> array(
    	   'post_type'	=> 'post',
    	   'post_status'=> 'publish'
    	),
       'field_groups'   => array(317, 123), // 317 is your existing field group and 123 is a madeup which contains the taxonomy field.
       'submit_value'       => 'Submit Post',
       'updated_message'    => 'Saved!'
    )); ?>
    

    You shouldn’t need to mess with $_POST parameters and filters.

  • Jonathan – thanks for all your assistance and hope everything is ok.

    Disregard this Post. I needed to check the save terms to post option for the taxonomy custom field

    I did as you recommended and the taxonomy does now show the categories that can be selected but these are not put to the post once it is created. I mean the form creates the new posts but they still list as Uncategorized in the post info. The ACF taxonomy field is keeping the selection from the fronted form but I am looking to use the standard WP categories like under each post the theme lists the category associated with that post.

    Thanks
    Andy

  • Hi Andy,

    Great that everything worked out! And that you found the setting for mapping the acf field to the post>term relationship 🙂

    Please feel free to open up any topic if you need further help.

  • I use this code in my template

    
    <?php acf_form(array(
     'post_id'	=> 'new_post',
     'new_post'	=> array(
       'post_type'	=> '',
       'post_status'=> 'publish',
       'post_title'  => $_POST['acf']['field_5882557e92006'].' - '.$_POST['acf']['field_5882557e9207c'].' - '.get_cat_name( $_POST['acf']['field_588713c7a61d9'] ), // Post Title ACF field key
       'post_category' => $_POST['acf']['field_588713c7a61d9'], // Post Category ACF field key
    ),
       'field_groups'   => array(98), // Create post field group ID(s)
       'form'               => true,
       'html_before_fields' => '',
       'html_after_fields'  => '',
       'submit_value'       => 'Submit Post',
       'updated_message'    => 'Saved!',
        'uploader' 			 => 'wp',
        'return' 		=> '/'
       )); ?>
    

    …and that in my function.php

    
    function acf_review_before_save_post($post_id) {
    	if (empty($_POST['acf']))
    		return;
    	$_POST['acf']['_post_title'] =  get_cat_name( $_POST['acf']['field_588713c7a61d9'] ).' - '.$_POST['acf']['field_5882557e92006'].' - '.$_POST['acf']['field_5882557e9207c'];
    	$_POST['acf']['_post_category'] = $_POST['acf']['field_588713c7a61d9'];
    	return $post_id;
    }
    add_action('acf/pre_save_post', 'acf_review_before_save_post', -1);
    ?>
    

    …and all work very well except for the _post_category.

    I whant to change the real category of the post but it dosen’t change after saving.

    What am I wrong?

    Thanks!

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

The topic ‘Selecting Category from Frontend Form’ is closed to new replies.