Support

Account

Home Forums Backend Issues (wp-admin) My Custom Field Deletes Itself…

Solving

My Custom Field Deletes Itself…

  • Hello Community,

    I want to use ACF to display Time Slots (for Real Estate Visits).
    I build the Base with many repeater fields, and everything looked fine.
    Then I grabbed data from a WordPress Custom Field / Custom Post Type and used it in a ACF Dropdown / Select Field.

    But now I have a very big Problem:

    If I change only a little bit on the Settings of my ACF Custom Fields and update the page, it deletes EVERYTHING. Everything is gone! I have to build all the fields again. This problem is only there, if my custom code is active.

    Please help me. Here is my Code, which is in my functions.php

    function numediaweb_custom_user_profile_fields($user) {
    ?>
    
    <?php
    $objektsave = array();
    $the_query = new WP_Query("post_type=immomakler_object&posts_per_page=-1&field=ids");    
        if ($the_query->have_posts()) {
          while ($the_query->have_posts()){
          	$the_query->the_post();
             $save = get_the_ID();
             $objektsave = get_post_meta($save,'objektnr_extern',true);
             $objekt[] = $objektsave;
    
          }
    
        }
    
     ?>
    
    <?php
    return $objekt;
    }
    add_action('show_user_profile', 'numediaweb_custom_user_profile_fields');
    add_action('edit_user_profile', 'numediaweb_custom_user_profile_fields');
    
    /// DATA IN ACF
          function acf_load_auswahlfeld_field_choices( $field ) {
      $objekt = numediaweb_custom_user_profile_fields();  
    
    $field['choices'][] = $objekt;
        
        return $field;
        
    }
    
    add_filter('acf/load_field/name=immoid', 'acf_load_auswahlfeld_field_choices');
  • You need to reset the global post object after you run your custom query

    
    function numediaweb_custom_user_profile_fields($user) {
      $objektsave = array();
      $the_query = new WP_Query("post_type=immomakler_object&posts_per_page=-1&field=ids");    
      if ($the_query->have_posts()) {
        global $post; // not sure if you need this or not, try it with and without
        while ($the_query->have_posts()){
          $the_query->the_post();
           $save = get_the_ID();
           $objektsave = get_post_meta($save,'objektnr_extern',true);
           $objekt[] = $objektsave;
    
        }
        wp_reset_postdata();
      }
      return $objekt;
    }
    
  • Hi John,

    thanks for your fast answer, but I still have the same issue. My code looks now this way:

    
    function numediaweb_custom_user_profile_fields($user) {
      $objektsave = array();
      $the_query = new WP_Query("post_type=immomakler_object&posts_per_page=-1&field=ids");    
      if ($the_query->have_posts()) {
        global $post; // not sure if you need this or not, try it with and without
        while ($the_query->have_posts()){
          $the_query->the_post();
           $save = get_the_ID();
           $objektsave = get_post_meta($save,'objektnr_extern',true);
           $objekt[] = $objektsave;
    
        }
        wp_reset_postdata();
      }
      return $objekt;
    }
    
    add_action('show_user_profile', 'numediaweb_custom_user_profile_fields');
    add_action('edit_user_profile', 'numediaweb_custom_user_profile_fields');
    
    /// DATA IN ACF
      function acf_load_auswahlfeld_field_choices( $field ) {
      $objekt = numediaweb_custom_user_profile_fields();  
    
    $field['choices'][] = $objekt;
        
        return $field;
        
    }
    
    add_filter('acf/load_field/name=immoid', 'acf_load_auswahlfeld_field_choices');
  • And if you remove the code then there is not problem?

    what part of it causes the problem? If you comment out the load_field add_filter line does the problem go away?

    To be honest, I’m really not sure by looking at your function why you would call it on the two hooks show_user_profile and edit_user_profile as these hooks are used to output HTML, which you’re not doing.

    You might try looping through the posts of your query without using have_posts, that will eliminate the query being the cause of the problem

    
    function numediaweb_custom_user_profile_fields($user) {
      $objekt = array();
      $the_query = new WP_Query("post_type=immomakler_object&posts_per_page=-1&field=ids");
      if (count($the_query->posts)) {
        foreach ($this_query->posts as $post) {
          $post_id = $post->ID;
          $objektsave = get_post_meta($post_id,'objektnr_extern',true);
          $objekt[] = $objektsave;
        }
      }
      return $objekt;
    }
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘My Custom Field Deletes Itself…’ is closed to new replies.