Support

Account

Home Forums General Issues User Meta Field not updating Taxonomy in Post

Unread

User Meta Field not updating Taxonomy in Post

  • Hi, I have a Make New Post Form where a Email Address, Telephone Number and Post Categories fields are entered.

    All fields then displayed on the post but also in User Profile as meta_fields. The user can then update fields which then update the post from there. The only field I can not get to update from the User profile is the Category selection which is the Taxonomy.

    
    
    acf_form_head();
    
    // Hook into ACF form submission
    add_action('acf/save_post', 'save_acf_form_data_to_user_meta', 20);
    
    function save_acf_form_data_to_user_meta($post_id) {
      // Check if this is the submission of the ACF form
      if (isset($_POST['acf'])) {
          // Get the submitted field values
          $field1_value = isset($_POST['acf']['field_662a232270ab5']) ? $_POST['acf']['field_662a232270ab5'] : ''; 
          $field2_value = isset($_POST['acf']['field_662a225d70ab4']) ? $_POST['acf']['field_662a225d70ab4'] : ''; 
    	  $field3_value = isset($_POST['acf']['field_663b690beecac']) ? $_POST['acf']['field_663b690beecac'] : ''; 
    
          // Get the post author ID
          $post_author_id = get_post_field('post_author', $post_id);
    
          // Update user meta for both fields
          if (!empty($field1_value)) {
              update_user_meta($post_author_id, 'email-1', $field1_value); 
          }
          if (!empty($field2_value)) {
              update_user_meta($post_author_id, 'phone-1', $field2_value); 
          }
    	   if (!empty($field3_value)) {
              update_user_meta($post_author_id, 'business_categories_new', $field3_value); 
          }
    		
      }
    }
    
    // Hook into the user profile update action
    add_action('profile_update', 'update_post_meta_from_user_meta');
    
    function update_post_meta_from_user_meta($user_id) {
      // Get the user meta field values
      $user_meta_field_value_1 = get_user_meta($user_id, 'email-1', true); 
      $user_meta_field_value_2 = get_user_meta($user_id, 'phone-1', true); 
      $user_meta_field_value_3 = get_user_meta($user_id, 'business_categories_new', true); 
    
      // Check if the user meta field values are not empty
      if (!empty($user_meta_field_value_1)) {
          // Get the post associated with the user
          $args = array(
              'post_type' => 'listing', 
              'author' => $user_id, // Filter posts by author (user)
              'post_status' => 'publish', // Change if necessary
              'numberposts' => 1 // Only retrieve one post
          );
    
          $posts = get_posts($args);
    
          // Check if a post is found
          if (!empty($posts)) {
              // Update the post custom meta fields with the user meta field values
              update_post_meta($posts[0]->ID, 'email-1', $user_meta_field_value_1); 
              update_post_meta($posts[0]->ID, 'phone-1', $user_meta_field_value_2); 
              update_post_meta($posts[0]->ID, 'business_categories_new', $user_meta_field_value_3); 
    
          }
      }
    }
    

    Any help much appreciated.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.