Home › Forums › General Issues › Fetching default image option
I implemented this code https://github.com/Hube2/acf-filters-and-functions/blob/master/default-image-for-image-field.php so that I can get the default image option in my image field.
The issue is that, say I’m on my home page where I am showcasing all the posts, in this case get_field( 'image_field' ) will fetch the image_field from the first post, and I’d like it to fallback to the default value.
How do I fetch that default image in this case?

I’ve been looking into this. As with most things in ACF, when a field has never been set you need to use the field key to get the field value. If you use the field key like
get_field('field_0123456');
then ACF will return the default value when a field has never been set.

Short of this you’ll need to resort to
$image = get_field('image');
if (!$image) {
// code here to set $image to default value
// you can probably use get_field_object($field_key)
// end get the setting for default_value
}
but it would probably be much easier to just use the key in get_field in the first place.
I will try it with the second option, I’m not sure that the first one is suitable for development (don’t know if the field numerical id will change). Thanks for the suggestion.

Not the ID (as in post ID that can change ) but the field key. The field key will not change unless you export to PHP and change it yourself or you delete the field and recreate it. The field key is a constant that can be depended on.
The topic ‘Fetching default image option’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.