Search code examples
phpadvanced-custom-fields

ACF frontend form with custom post types and Custom taxonomy tags


I am not very experienced in PHP. This may be an easy question to ask. I am not able to find the answer to my question.

I have a Custom Post Type (created using CPT UI), food_type and a custom taxonomy (category): meal_cat. And also tags (tags) to select from like breakfast, lunch, dinner etc. The food_type has a title, a description and a media field (ACF fields).

When a new food_type needs to be created, it is necessary for it to have the title, description, and a media. It is also required that the meal_cat be selected from a list as well.

I need a frontend form that can work with my custom post type and custom taxonomy in the following manner:

  1. In the frontend upload form, I need to load the meal_cat for the user to select from (for example in a drop-down list).
  2. The form should also include a title and description area and the upload button
  3. When a file is selected and the button is pressed, a new food_type post should be created (with the selected meal_cat and the given title and description) and the uploaded media should be attached to this new post (food_type).

I'm able to get food_type, title and description created but not able to get the meal_cat.

There is a plugin that has the capability to accomplish what I want (Front-end Uploader)

But I want to code and create this without the plugin.

my Function.php code

    /**
      * Create new post via ACF frontend
    */

    function my_pre_save_post( $post_id )
    {
        
        $custom_tax = array(
        'color' => array(
                'breakfast',
                'lunch',
                'dinner'
            )
        );

        // Create a new post
        $post = array(
            'post_status'  => 'publish',
            'post_title'  => $_POST['fields']['field_5a58f487fbc67'],
            'post_content'  => $_POST['fields']['field_5a58f49bfbc69'],
            'post_type'  => 'food',
            'tax_input'  => array( 'meal' => array(2,3,4)), //taxonomies
        ); 

        // insert the post
        $post_id = wp_insert_post($post);   

    // save a Form Date value
    $field_key = "field_5a58f491fbc68";
    $value = $_POST['fields']['field_5a58f491fbc68'];
    update_field( $field_key, $value, $post_id );

    // save a Select value
    $field_key = "field_5a59ced6c6c4a";
    $value = array("breakfast", "lunch", "dinner");
    update_field( $field_key, $value, $post_id );

    // save a Form Author value
    $field_key = "field_5a58f6601d48b";
    $value = $_POST['fields']['field_5a58f6601d48b'];
    update_field( $field_key, $value, $post_id );
        
        wp_redirect(get_permalink($post_id)); exit;

        // return the new ID
        return $post_id;
    }
      
    add_filter('acf/pre_save_post' , 'my_pre_save_post' );

And Frontend Form code:

         <?php

            $args = array(
                'post_id' => 'new',
                'field_groups' => array(4),
                'submit_value' => 'Publish Post',
            );
             
            acf_form( $args );
         
        ?>

Solution

  • For any one wants, I was able to achieve by adding this code the function.php file

    // save a Select value  
    $field_key = "categorytax"; //Field Name
    $value = $field['value'];
    $term_taxonomy_ids = wp_set_object_terms( $post_id, $value, 'meal' );
    $tag = array($_POST['fields']['field_5a59ced6c6c4a']); // Correct. This will add the tag with the id as submitted.
    wp_set_post_terms( $post_id, $tag, 'meal' ); 
    

    The above code will add the custom taxonomy tag from the fronted form's select field.