Add new post in CPT category
-
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:
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
- The topic ‘Add new post in CPT category’ is closed to new replies.