• 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;
    }

    The page I need help with: [log in to see the link]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Aurovrata Venet

    (@aurovrata)

    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

    (@aurovrata)

    I am assuming this is now resolved since I have not heard back from you.

    Thread Starter Atmospheric Advantage LLC

    (@atmovantage)

    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.ww.wp.xz.cn/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.ww.wp.xz.cn/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

    (@aurovrata)

    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

    (@atmovantage)

    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

    (@aurovrata)

    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.