• Resolved beelinedigital

    (@beelinedigital)


    Hi,

    Is there a way to add a ‘create post’ form that will then automatically add the posts created to a specific category, rather than having to select a category within the form?

    Thanks in advance,

Viewing 4 replies - 1 through 4 (of 4 total)
  • Hi @beelinedigital

    I hope you are doing good today.

    This should be possible, please follow this solution:
    https://ww.wp.xz.cn/support/topic/forminator-post-data-only-specific-article-categories-allowed/

    Kind Regards,
    Kris

    Thread Starter beelinedigital

    (@beelinedigital)

    Thank you for your reply.

    I am looking to create 3-4 forms but only want to show 1 category option for each form.

    So for example, form 1 creates posts for category ‘X’.

    Also is it possible to set it so posts are automatically published.

    Plugin Support Patrick – WPMU DEV Support

    (@wpmudevsupport12)

    Hi @beelinedigital

    I hope you are doing well.

    Do you need to show the category on the front end?

    If not,

    Could you try this workaround that I believe fits well here?

    Add this code as a mu-plugin:

    <?php
    
    add_action( 'forminator_post_data_field_post_saved', function( $post_id, $field, $data ){
    
      $category = (int) $_POST['hidden-1'];
      // $categories = [ 1, 2, 3 ];
    
      if( $category ){
        wp_set_post_categories( $post_id, $category );
        //wp_set_post_categories( $post_id, $categories );
      }else{
        // Default category if no category is defined on hidden field, this conditional can be removed.
        wp_set_post_categories( $post_id, [ 157 ] );
      }
    
    }, 99, 3);

    Create a hidden field, set it to a custom value and then set the category ID that you would like to use.

    https://monosnap.com/file/jM76xzMoknnr2RMLmz7P8wu9bTn7ja

    Remove the default category field in Post data, and then on form submission, it will set to that custom category on the hidden field.

    You can also extend the code to multiple categories or more conditionals, in case you would like to show it on the frontend then you could add the category as a Select field or regular HTML field.

    To learn more about how mu-plugin works https://wpmudev.com/blog/wordpress-mu-plugins/

    Also is it possible to set it so posts are automatically published.

    It is, you can change the field setting:

    https://monosnap.com/file/mxyNG6PqHLg70CMyP8IQVvfEJPKEbJ

    Best Regards
    Patrick Freitas

    Plugin Support Amin – WPMU DEV Support

    (@wpmudev-support2)

    Hello @beelinedigital ,

    We haven’t heard from you for some time now, so it looks like you don’t have more questions for us.

    Feel free to re-open this ticket if needed.

    Kind regards
    Kasia

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Create Post Form – Category’ is closed to new replies.