• Resolved sslv

    (@sslv)


    On my WP project I’m using Advanced Custom Fields (Version 6.2.6.1 | By?WP Engine) and Frontend Admin (Version 3.18.4 | By?Shabti Kaplan). I’m using the free version for both plugins!

    Using ACF plugin , I managed to create:

    • CPT (Masterclasses)
    • Taxonomies (Apprentices, Junior Barbers, Senior Barbers, Master Barbers, Shop Owners)
    • Form Group (in the form group I have a drop down select to allow users to select specific CPT Taxonomy:
    The ACF Form Field for Taxonomies. Notice the Taxonomy is set to Masterclass Category, which allow only the options specified above

    Using the Frontend Admin plugin I managed to create a form and assign the ACF Field Group with a submit button:

    So I can now display the form on frontend using ACF and Frontend Admin plugins:

    THE ISSUE:

    Even though the user is able to select a taxonomy during the form, once submitted the CPT goes to the Masterclasses (and not in WP Default Posts) but is not assigned to any category at all!

    I have searched through both plugin settings and I’m not able to find if this feature is built-in or not! As per my understanding is not available in the free version! I have also tried some php code which I found online with no success! See example 1-3 below:

    Example 1:

    // Hook into Frontend Admin form submission
    add_action('fa_after_save_post', 'assign_post_to_category_on_form_submission', 10, 2);
    
    function assign_post_to_category_on_form_submission($post_id, $post_data) {
    
        if (isset($post_data['field_65bca532322d9'])) { 
            $category_id = intval($post_data['field_65bca532322d9']); 
    		
            // Assign the post to the selected category
            wp_set_post_categories($post_id, array($category_id));
        }
    }

    Example 2:

    add_action('acf/save_post', 'assign_post_to_category_on_form_submission');
    
    function assign_post_to_category_on_form_submission($post_id) {
    
        // Check if plugin is active
        if (function_exists('is_plugin_active') && is_plugin_active('frontend-admin/frontend-admin.php')) {
            global $post;
    		
            if ($post && $post->post_type === 'masterclass' && isset($_POST['acf'])) {
                $category_id = isset($_POST['acf']['field_6329efas2109f0']) ? intval($_POST['acf']['field_6329efas2109f0']) : 0;
    			
                if ($category_id) {
                    wp_set_post_categories($post_id, array($category_id));
                }
            }
        }
    }

    Example 3:

    add_action('save_post', 'assign_masterclass_taxonomy_on_form_submission', 10, 2);
    
    function assign_masterclass_taxonomy_on_form_submission($post_id, $post) {
    	
        // Check if this is an ACF form submission for Masterclasses
        if ($post && $post->post_type === 'masterclasses' && isset($_POST['acf'])) {
            $fields = $_POST['acf']; // Get submitted form fields from ACF
    
            if (isset($fields['field_6312412f70921']) && !empty($fields['field_6312412f70921'])) {
                $term_id = intval($fields['field_6312412f70921']); // Get the selected term ID
    
                // Assign the term to the post
                wp_set_post_terms($post_id, $term_id, 'masterclasses', false);
            }
        }

    REQUIRED SOLUTION:

    When the user selects the preferred taxonomy and eventually submits the form, I want during that process to also assign the category to the post. I’m not sure exactly if I should first save the post and then assign it to a category or if there is another way. If you know how to do that please let me know

Viewing 2 replies - 1 through 2 (of 2 total)
  • ACF Support

    (@acfsupport)

    Hi there!

    ACF Support Team here. This forum is generally used by ACF users to help each other out.

    However, we would love to continue investigating and troubleshooting this issue, please can you create a ticket using our ?support form and we can look into it further.

    Thread Starter sslv

    (@sslv)

    Hi all, this is how I solved the issue in case anyone is seeking solution:

    add_action('frontend_admin/save_post', 'my_frontend_save_post', 10, 2);
    function my_frontend_save_post($form, $post_id) {
    	
    	$post = get_post( $post_id ); //Get Post Information
    	
    	if ( $post ) {
    		$post_type = $post->post_type; //Get Post Type
    		$selected_category_ids = array(); //Declasre Category ID
    		$taxonomy_key = ''; //Declare Taxonomy Key
    		
    		if($post_type == "masterclass"){
    			//Get Masterclass Categories
    			$selected_category_ids = get_field("field_65bca5d0bb5d9", $post_id);
    			$taxonomy_key = 'masterclass-category';
    		}
    		elseif($post_type == "job-offer"){
    			$selected_category_ids = get_field("field_6329e12f709f0", $post_id);
    			$taxonomy_key = 'job-offer-category';
    		}
    		
    		// Retrieve category names based on category IDs
    		if (!empty($selected_category_ids)) {
    			wp_set_post_terms($post_id, $selected_category_ids, $taxonomy_key, false);
    		}
    
    		send_email(
    			get_the_author_meta('user_email', get_post_field ('post_author', $post_id)), //Author Email
    			get_the_title($post_id), //Post Title
    			get_permalink($post_id), //Post URL
    			$post_type //Post Type (Masterclass or Job Offer)
    		);
    		exit;
    	}
    }

    For the above code I’m using “frontend_admin/save_post” action hook. This allows me to capture the event once the form has been submitted! After getting some post information I added logic to check according to the user selection in which category the post would go. In my case “masterclass” or “job-offer”!

    After that I use the “wp_set_post_terms” to force the post to a category and lastly send an email to the author. I have some additional logic while constructing the email but there are many example online so I skipped adding that part!

    Hope it helps you! ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add new post in CPT category’ is closed to new replies.