Home › Forums › Add-ons › Gallery Field › Convert Attachments to Gallery › Reply To: Convert Attachments to Gallery
You could create an acf/load_field filter https://www.advancedcustomfields.com/resources/acfload_field/ or an acf/load_value filter https://www.advancedcustomfields.com/resources/acfload_value/. You could then use the WP get_attached_media() function https://codex.wordpress.org/Function_Reference/get_attached_media to get all of the media for a post.
add_filter('acf/load_value/name=your-field-name', 'build_gallery_from_attachments', 10, 3);
function build_gallery_from_attachments($value, $post_id, $field) {
if (!empty($value)) {
// already has a value
return;
}
// does not have a value, build value from attachments
$media = get_attached_media('image', $post_id);
$value = array();
if ($media) {
foreach ($media as $image) {
$value[] = $image->ID;
}
}
return $value;
}
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.