will_a
asked 6 years ago

Hi, I am trying to make the questions and answers submitted on the front end of the site default to a specific category.
I have started by enabling categories (so not Q&A custom post type categories, but WP post categories) for the custom post types via this code. It seems to work fine for this. 
// adding possibilities to have categories for custom post types
public function gt_custom_post_type_categories() {
register_taxonomy_for_object_type( \\\’category\\\’, \\\’dwqa-question\\\’ );
register_taxonomy_for_object_type( \\\’category\\\’, \\\’dwqa-answer\\\’ );
}
add_action( \\\’init\\\’, \\\’gt_custom_post_type_categories\\\’, 11 );
And then use this code for assigning the categories on save / publish (it works for things on the back end, but not the front).
// setting default post catagory when saving
public function gt_set_default_category( $post_id, $post, $update ) {
// Slugs of the custom post types
$slugs = array(\\\’dwqa-question\\\’, \\\’dwqa-answer\\\’);
// 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 \\\’dwqa-question\\\’:
$default_category = \\\’teaching-hall-questions\\\’; // does not hook in on the fron end
break;
case \\\’dwqa-answer\\\’:
$default_category = \\\’teaching-hall-answers\\\’; // 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 );

 
Do you know how I can get this to work on the front end too? What to hook into? Does it need to be a filter of some sore instead? If so, what form should it take or can you either tell me explicitly how or send me a link (I am not as experience with working with filters as actions.

Thank you so much!
 

1 Answers
DominicStaff
answered 6 years ago

 
You can override the template of the submit question form. To do so, you copy this file question-submit-form.php: http://snippi.com/s/fui8w3c  into your-theme/dwqa-templates folder. And go to line 36 and replace the selected category ID that you want to choose the default.
To find the category ID, In backend, when you edit a category, you will see your category ID in the URL on your browser address bar which looks like this: “…&tag_ID=”
Also, you should create a new folder in the theme folder with the name is: dwqa-templates
the custom won’t override when you update the plugin.
 

Powered by DW Question & Answer Pro