Set Default Term for Specific Post Submission Form

Support MB Frontend Submission Set Default Term for Specific Post Submission FormResolved

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #45486
    Rebecca RobertsonRebecca Robertson
    Participant

    I 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.

    #45494
    PeterPeter
    Moderator

    Hello 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 attribute id). 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
        }
    } );
    #45511
    Rebecca RobertsonRebecca Robertson
    Participant

    Awesome! Thank you!

    #46482
    AnLipAnLip
    Participant

    Hi 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 );
        }
    } );
    #46483
    AnLipAnLip
    Participant

    I also tried with hook "rwmb_frontend_before_save_post' but the fatal error is still caused.

    #46790
    Rebecca RobertsonRebecca Robertson
    Participant

    Still no joy with this.

    #47152
    Rebecca RobertsonRebecca Robertson
    Participant

    Please, are there any other ideas for this? I would really like to be able to make this work.

    #47376
    Rebecca RobertsonRebecca Robertson
    Participant

    Since 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?

    #47377
    Rebecca RobertsonRebecca Robertson
    Participant

    For others still looking for an answer here, this topic might help. I haven't tried it yet, but it seems to be better explained.

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.