Support

Account

Home Forums Backend Issues (wp-admin) How To Update Select Field Reference

Solved

How To Update Select Field Reference

  • Hi there,

    I have a select/choices field ‘map_marker’ which is populated dynamically using the acf/load_field filter. It references a db table created by another plugin (leaflet mapsmarker) like so:

    
    add_filter('acf/load_field/name=map_marker', 'my_acf_mapsmarker_choices');
    
    function my_acf_mapsmarker_choices($field){
    
      $field['choices'] = array();
    
      global $wpdb;
    
      $results = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'leafletmapsmarker_markers ORDER BY ID ASC');  
    
      if(!empty($results)) :
    
      foreach($results as $result) :
    
        $value = $result->id;
        $label = $result->markername;
        $field['choices'][ $value ] = $label;
    
      endforeach;
      endif;
      return $field;
    }
    

    I have thousands of posts which each have a select field with an entry.

    The problem:

    In the latest release of the Maps Marker plugin the author has changed the db table name that this select field references.

    Note, the update process from old plugin to new does not involve removing the older plugin but it does need to be deactivated. The old db table and its entries will remain. The IDs of each dropdown item will stay the same so what I need to do is update the table reference whilst keeping the existing entries in all my posts intact.

    Is this possible? Is there a simple way to do this, via an SQL query or will I have to go into each post and update manually?

    Many thanks in advance for any help!

  • you should be able to just change the table name in your query. As long as the “values” are still the same everthing should continue to work.

  • What happens if you just changed the table name in your filter’s SQL query?

    $results = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'NEW_TABLE_NAME ORDER BY ID ASC');

    If the ID values remain the same then it should work seamlessly.

    Let me know if I misunderstood.

  • This did the trick! Cheers guys. Not sure who to credit but Johns response was just a tiny bit earlier 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.