Title: Help Setting Post Categories
Last modified: August 3, 2018

---

# Help Setting Post Categories

 *  Resolved [Atmospheric Advantage LLC](https://wordpress.org/support/users/atmovantage/)
 * (@atmovantage)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/help-setting-post-categories/)
 * I’ve been able to map all the custom meta fields however I’m having a lot of 
   trouble mapping the post categories and post tags. In my contact form I have 
   a select dropdown menu (field name is “platform”) with options for existing post
   categories “pc” “ps4” and “xbox”.
 * In the Taxonomies section when I try to map Post Categories to the “platform”
   field it looks right but when the form is submitted the category won’t be selected.
   I’ve read through the documentation and even tried rolling back CF7 to version
   4.7 and it still just refuses to map anything to the Post Categories taxonomy.
   Any help with this would be greatly appreciated!
 * I assume in my functions php file I’ll need to use the Default Terms Values Filter.
   I’ve tried to modify the filter for my purposes but so far it’s not working. 
   My code is below:
 *     ```
       add_filter('cf7_2_post_filter_cf7_taxonomy_terms', 'modify_my_terms',10,4);
       /**
       * Function to pre-fill form dropdown/radio/checkbox fields mapped to a taxonomy.
       * Hooked to 'cf7_2_post_filter_cf7_taxonomy_terms'.
       * @param array $terms_id initially an empty array.
       * @param $cf7_id  the form id being loaded.
       * @param $field  the field name for which the value is being filtered.
       * @param string $cf7_key unique key identifying your form.
       * @return array an array of term IDs to select.
       */
       function modify_my_terms($terms_id, $cf7_id, $field, $cf7_key){
         /*This filter allows you to pre-fill/select taxonomy terms fields for new submissions.*/
         //assuming you have defined a checkbox field called city-locations for cf7 form 'contact-us'.
         if('user-submitted-address' == $cf7_key & 'platform' == $field){
           $term = get_term_by('name', $field ,'category');
           $terms_id = array();
           $terms_id[] = $term->term_id;
         }
         return $terms_id;
       }
       ```
   
    -  This topic was modified 7 years, 10 months ago by [Atmospheric Advantage LLC](https://wordpress.org/support/users/atmovantage/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fhelp-setting-post-categories%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Plugin Author [Aurovrata Venet](https://wordpress.org/support/users/aurovrata/)
 * (@aurovrata)
 * [7 years, 10 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10577788)
 * have you tried without using the ‘cf7_2_post_filter_cf7_taxonomy_terms’ filter?
 * any terms selected in the form will automatically be tagged to the saved post.
 *  Plugin Author [Aurovrata Venet](https://wordpress.org/support/users/aurovrata/)
 * (@aurovrata)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10944862)
 * I am assuming this is now resolved since I have not heard back from you.
 *  Thread Starter [Atmospheric Advantage LLC](https://wordpress.org/support/users/atmovantage/)
 * (@atmovantage)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10944914)
 * Hi Aurovrata,
 * Thanks for your response. It’s been a while since I implemented the fix but I
   believe your suggestion put me on the right track. I ended up using the following
   code to set my post categories based on the user’s selections on the form:
 *     ```
       add_filter('cf7_2_post_filter-category','filter_category',10,3);
       function filter_category($value, $post_id, $form_data){
         //$value is the post field value to return, by default it is empty. If you are filtering a taxonomy you can return either slug/id/array.  in case of ids make sure to cast them integers.(see https://codex.wordpress.org/Function_Reference/wp_set_object_terms for more information.)
         //$post_id is the ID of the post to which the form values are being mapped to
         // $form_data is the submitted form data as an array of field-name=>value pairs
   
        if (isset($form_data['platforms'])) {
               $value[] = $form_data['platforms'];
             }
         return $value;
       }
       ```
   
 * Likewise, I also had success in getting post tags to map automatically using 
   the following code:
 *     ```
       add_filter('cf7_2_post_filter-post_tag','filter_post_tag',10,3);
       function filter_post_tag($value, $post_id, $form_data){
         //$value is the post field value to return, by default it is empty. If you are filtering a taxonomy you can return either slug/id/array.  in case of ids make sure to cast them integers.(see https://codex.wordpress.org/Function_Reference/wp_set_object_terms for more information.)
         //$post_id is the ID of the post to which the form values are being mapped to
         // $form_data is the submitted form data as an array of field-name=>value pairs
   
         if (isset($form_data['biome'])) {
               $value[] = $form_data['biome'];
             }
         return $value;
       ```
   
 *  Plugin Author [Aurovrata Venet](https://wordpress.org/support/users/aurovrata/)
 * (@aurovrata)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10945010)
 * great. What type of fields are `platforms` and `biome`?
 * If you implement them as radios (with no options set) and map it to your taxonomy,
   the plugin will prefill the radio field with the terms of the taxonomy, and allow
   your your users to select only 1 term. This can also be done with a dropdown 
   field.
 * Alternatively you use a checkbox field which allows multiple terms to be selected.
 *  Thread Starter [Atmospheric Advantage LLC](https://wordpress.org/support/users/atmovantage/)
 * (@atmovantage)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10945095)
 * In the examples above both of the fields were required drop-down selections on
   the form. Although I also had success in mapping checkboxes on the form to tags
   as well.
 *  Plugin Author [Aurovrata Venet](https://wordpress.org/support/users/aurovrata/)
 * (@aurovrata)
 * [7 years, 6 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10945139)
 * > above both of the fields were required drop-down selections
 * are the dropdowns populated with your terms? If a user selects an option, does
   the saved post not automatically get assigned to the selected tern?

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

The topic ‘Help Setting Post Categories’ is closed to new replies.

 * ![](https://ps.w.org/post-my-contact-form-7/assets/icon-256x256.png?rev=1985682)
 * [Post My CF7 Form](https://wordpress.org/plugins/post-my-contact-form-7/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/post-my-contact-form-7/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/post-my-contact-form-7/)
 * [Active Topics](https://wordpress.org/support/plugin/post-my-contact-form-7/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/post-my-contact-form-7/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/post-my-contact-form-7/reviews/)

## Tags

 * [solution](https://wordpress.org/support/topic-tag/solution/)

 * 6 replies
 * 2 participants
 * Last reply from: [Aurovrata Venet](https://wordpress.org/support/users/aurovrata/)
 * Last activity: [7 years, 6 months ago](https://wordpress.org/support/topic/help-setting-post-categories/#post-10945139)
 * Status: resolved