Skip to:
Content
Pages
Categories
Search
Top
Bottom

Setting default categories on front end submission


  • willallen83
    Participant

    @willallen83

    Hi!

    I am trying to make any topics and replies submitted on the front end have a default category (I have extended the custom post type of bbPress to support categories). This works for everything submitted on the back end, but not everything submitted on the front end. I have played around with this a lot, and have not made any progress.

    I am using WordPress 4.9.5, bbPress 2.5.14 and the site I am working on (locked so this link probably won’t do any good) is https://gateway.aishasalem.com

        // adding possibilities to have categories for custom post types
        public function gt_custom_post_type_categories() {
          register_taxonomy_for_object_type( 'category', 'topic' );
          register_taxonomy_for_object_type( 'category', 'reply' );
        }
        add_action( 'init', 'gt_custom_post_type_categories', 11 );

    And this code to assign the categories as default when a post is saved / published. It works on the back end, but only partially on the front end.

        // setting default post catagory when saving
        public function gt_set_default_category( $post_id, $post, $update ) {
    
            // Slugs of the custom post types
            $slugs = array('topic', 'reply');
            // current post type
            $current_post_type = $post->post_type;
    
            // If this post isn't a custom posty type, don't update
            if ( !in_array($current_post_type, $slugs) ) {
              return;
            }
    
            // Sets the default category depending on the current post type
            switch ($current_post_type) {
              case 'topic':
                $default_category = 'livingroom-topics';
                break;
              case 'reply':
                $default_category = 'livingroom-replies';   // does not hook in on the fron end
                break;
              default:
                return;
            }
    
            // sets the default category
            $default_term = get_term_by('slug', $default_category, 'category');
            wp_set_object_terms(get_the_ID(), $default_term->term_id, 'category');
        }
        add_action( 'save_post', 'gt_set_default_category', 9, 3 );

    I have tried hooking into ‘bbp_new_reply’ but this doesn’t seem to help (maybe I am doing it wrong). Do you have any advice of how to do this? Where to hook into? If it should be a filter and a function of a different form (if so, please give as much info as possible, I am new to php wordpress and especially filters)?

    Thank you so much!!

  • You must be logged in to reply to this topic.
Skip to toolbar