Set Default Term for Specific Post Submission Form
Support › MB Frontend Submission › Set Default Term for Specific Post Submission FormResolved
- This topic has 8 replies, 3 voices, and was last updated 3 months ago by
Rebecca Robertson.
-
AuthorPosts
-
May 21, 2024 at 10:35 PM #45486
Rebecca Robertson
ParticipantI have several post submission forms for different types of content on my site. For some of them I need the submitted post to be set to a specific custom taxonomy term by default.
For instance, I have a custom post type called "Gear" with a custom taxonomy of "Gear Post Types" and there are three gear post types: guide, review, roundup. As the content for each type is very different, each Gear Post Type has it's own form.
I need the form for Gear Reviews to have the term "review" assigned by default.
I've tried adding a taxonomy field and using std to set the default term, and as I don't want the user to see the field I hide it with CSS. The problem is that when I hide the field, the term doesn't stay set.
I've also tried to use the rwmb_frontend_after_save_post function but it saves all Gear posts as reviews, not just those that use the gear review form.
I've tried to figure out how to use the hidden field to set the term, but for the life of me I can't figure out how to use that particular field.
Something this simple shouldn't be so difficult to do, any help at all would be greatly appreciated.
May 22, 2024 at 9:48 PM #45494Peter
ModeratorHello Rebecca,
If you use the action hook
rwmb_frontend_after_save_post
, you can check if the post is submitted from a specific form by checking the field group ID (shortcode attributeid
). For example:add_action( 'rwmb_frontend_after_save_post', function( $config ) { // Make sure you work on the correct form. if ( 'field-group-id' !== $config['id'] ) { //add your code here to set the term } } );
May 24, 2024 at 1:11 AM #45511Rebecca Robertson
ParticipantAwesome! Thank you!
September 17, 2024 at 2:44 PM #46482AnLip
ParticipantHi Peter, I'm trying your logic out but I get fatal errors crashing the site.
This is my code, would it work ?
add_action( 'rwmb_frontend_after_save_post', function( $config ) { // Make sure we're working on the correct form if ( 'actualites-gf' === $config['id'] ) { // Get the post ID $post_id = $config['post_id']; // Get the 'actualite' category ID $category_id = get_cat_ID('actualite'); // Add the category to the post wp_set_post_categories( $post_id, array( $category_id ), true ); } } );
September 17, 2024 at 2:50 PM #46483AnLip
ParticipantI also tried with hook "rwmb_frontend_before_save_post' but the fatal error is still caused.
October 30, 2024 at 10:39 PM #46790Rebecca Robertson
ParticipantStill no joy with this.
December 10, 2024 at 7:27 PM #47152Rebecca Robertson
ParticipantPlease, are there any other ideas for this? I would really like to be able to make this work.
January 12, 2025 at 11:56 PM #47376Rebecca Robertson
ParticipantSince the code given doesn't work, is it possible to use a hidden field? Since the documentation on hidden fields is limited and vague, can someone please explain how to set a category in a hidden field?
January 13, 2025 at 12:07 AM #47377Rebecca Robertson
ParticipantFor others still looking for an answer here, this topic might help. I haven't tried it yet, but it seems to be better explained.
-
AuthorPosts
- You must be logged in to reply to this topic.