Support Forum
Support › MB Frontend Submission › Using a Custom Taxonomy to Set Post Type
I have several post types on my site, and as all of them have more shared fields than different ones, I would like to add posts to any of these post types using a single form. However, since I can't select post types on the form, I have created a custom taxonomy called content_type and added terms related to the post types.
I want to save the post to the post type that corresponds to the custom taxonomy term when the post is submitted. In other words, if the user selects "Flora" as the content type, the post is saved to the "Flora" post type or if they choose "Fauna" as the content type, the post is saved to the "Fauna" post type.
I found this topic that is somewhat similar to my situation but am unsure about how to alter and implement the code for my needs.
Unlike the mentioned topic, I don't need or want to hide this particular taxonomy field.
Sorry, this topic is also very similar to what I want to do. Again, how would I change the code to work for my needs?
Hello Rebecca,
I understand that you want to set the post type based on the select field after submitting the frontend submission form. I share with you an example of how to make it work:
1. I have a field group and a simple select field with the options is the CPT slug and label:
[
'name' => __( 'Content type', 'your-text-domain' ),
'id' => 'content_type',
'type' => 'select',
'options' => [
'book' => __( 'Book', 'your-text-domain' ),
'post' => __( 'Post', 'your-text-domain' ),
'page' => __( 'Page', 'your-text-domain' ),
],
],
2. This field group is added to the frontend form.
3. I use this code to update the post type of the submitted post based on the select:
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
if ( isset( $_POST['content_type'] ) ) {
$post_type = $_POST['content_type'];
$my_post = [
'ID' => $post_id,
'post_type' => $post_type
];
wp_update_post( $my_post );
}
}, 10, 2 );
Refer to the documentation
https://docs.metabox.io/extensions/mb-frontend-submission/#form-hooks
https://developer.wordpress.org/reference/functions/wp_update_post/
Note: this is an example. If it doesn't work or does not meet your requirements, you can contact us here to request a customization service https://metabox.io/contact/
Thank you, Peter.
I'm already using the select field for content_type in my form, it's just that all of my posts are being saved as standard posts, which is the bit of nuisance I'm trying to clear up.
Your code looks like it should meet my needs, but let me make certain I have this correct, as my PHP is not nearly as good as I wish it were.
As I gave the post types and the content types the same name but not the same slugs, let's assume I have two post types, Flora(flora-post) and Fauna(fauna-post), with corresponding content_type terms Flora(flora-post-type) and Fauna(fauna-post-type) would I do this:
add_action( 'rwmb_frontend_after_process', function( $config, $post_id ) {
if ( isset( $_POST['flora-post-type'] ) ) {
$post_type = $_POST['flora-post'];
$my_post = [
'ID' => $post_id,
'post_type' => $flora-post
];
wp_update_post( $my_post );
} else if ( isset( $_POST['fauna-post-type'] ) ) {
$post_type = $_POST['fauna-post'];
$my_post = [
'ID' => $post_id,
'post_type' => $fauna-post
];
}, 10, 2 );