Home Forums General Issues get_field returning null after update Reply To: get_field returning null after update

  • As mentioned by @mattshaw if you are using ACF to manage fields created by some other plugin or value then it is usually a bad idea to do so without creating an ACF field to replace the other field and then hide the other field so that it cannot be used.

    As an example, on every site I built I replace the built in WP featured image field. I do this so that I can require a feature image because the front end design depends on the featured image existing. I create a field with the label “Featured Image” and the name “_thumbnail_id” which is identical to the meta key used by WP for holding the featured image ID. Then I hide the standard featured image input field. This makes it so that I can use get_field('_thumbnail_id') and also makes the value available to all standard WP functions that deals with the featured image.

    I can understand how this can lead to problems for some but the developers of ACF are enforcing what would be considered best practices. get_field() should not be used for getting meta values for fields created outside of ACF. get_{$object_type}_meta() should have been using all along in these cases.

    Another reason of not using get_field() for non ACF fields is simply readability and needing to figure out code in the future. If I see get_field('field name') I and other developers will automatically think that this is an ACF field and I will start looking for this field in the field groups if I need to change something. I will be completely confused when I don’t find that field. It will make the code much harder for others to work on or even myself a year after I’ve written it.

    Another thing to consider is that ACF fields should always be defined no matter how/when they will be used and other methods of showing and hiding those fields should be used rather than conditionally creating the fields. This is one of the uses for the acf/prepare_field filter which can be used to conditionally include a field when showing a field group.