Home › Forums › Quform WordPress › Custom Post Type with Custom Taxonomy
- This topic has 3 replies, 2 voices, and was last updated 5 years, 9 months ago by Ally.
- AuthorPosts
- June 11, 2018 at 1:02 pm #26014marowiParticipant
Hello,
I have a problem with submitting as custom post type with custom taxonomies.
I have already managed to select from the existing custom taxonomy terms via “Select Menu” but the selected terms are not assigned to the custom post type after publishing.
// Creating a custom post type
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
$title = $form->getValue('quform_1_16');
$content = $form->getValue('quform_1_14');
$post = array(
'post_title' => $title,
'post_content' => $content,
'post_type' => 'catch',
'post_status' => 'publish'
);
wp_insert_post($post);
return $result;
}, 10, 2);
// Adds terms from "location" to Quform
add_action('quform_pre_display_1', function (Quform_Form $form) {
$element = $form->getElement('quform_1_7');
if ($element instanceof Quform_Element_Select) {
$options = array();
$terms = get_terms(array('taxonomy' => 'location', 'orderby' => 'count', 'fields' => 'all'));
foreach ($terms as $term) {
$options[] = array('label' => $term->name, 'value' => $term->slug, 'id' => $term->term_id);
}
$element->setOptions($options);
}
});
The custom post is published but without connected taxonomies.
Is there something I have to change in the settings?
Best regards
ManuelJune 11, 2018 at 1:40 pm #26015marowiParticipantEdit:
When I view the entry under “Forms > Entries” I can see all selected terms.
But they were not handed over to the published custom post type.
June 12, 2018 at 2:21 pm #26028marowiParticipantProblem solved. 🙂
Here is what worked for me:
// Creating a custom post type
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
$title = $form->getValue('quform_1_16');
$content = $form->getValue('quform_1_14');
$location = $form->getValue('quform_1_7');
$post = array(
'post_title' => $title,
'post_content' => $content,
'post_type' => 'catch',
'post_status' => 'publish',
'tax_input' => $location
);
$post_id = wp_insert_post($post);
wp_set_object_terms($post_id, $location, 'location');
return $result;
}, 10, 2);
June 12, 2018 at 3:57 pm #26039AllySupport StaffYou don't have permission to view this content. Please log in or register and then verify your purchases to gain access.
- AuthorPosts
- You must be logged in to reply to this topic.