Hi @ruenel,
I have found a solution to the second question I asked in the secure note. This actually provides a better solution than for my original question.
What this function does is when an image is added to an ACF Pro gallery in a post, the image(s) are replicated into the parent categories’ ACF Pro galleries. The images are also removed from the category galleries if they have been removed from the post.
// =================================================================
// POPULATE ACF GALLERY IMAGES FROM POST TO CATEGORY
// =================================================================
// Function to get the image IDs from a mill's ACF gallery
function get_mill_gallery_image_ids($mill_id) {
$image_ids = array();
$images = get_field('mill_gallery', $mill_id);
if ($images) {
foreach ($images as $image) {
$image_ids[] = $image['ID']; // Get image IDs
}
}
return $image_ids;
}
// Function to replicate mill images to categories
function replicate_mill_images_to_categories() {
// Get all categories for the 'mill_location' taxonomy
$categories = get_terms(array(
'taxonomy' => 'mill_location',
'hide_empty' => false,
));
if ($categories && !is_wp_error($categories)) {
foreach ($categories as $category) {
// Get all posts within the current category
$posts_in_category = get_posts(array(
'post_type' => 'mill',
'tax_query' => array(
array(
'taxonomy' => 'mill_location',
'field' => 'term_id',
'terms' => $category->term_id,
),
),
'posts_per_page' => -1,
));
$combined_images = array(); // Initialize array to store all image IDs
foreach ($posts_in_category as $post) {
$mill_image_ids = get_mill_gallery_image_ids($post->ID);
$combined_images = array_merge($combined_images, $mill_image_ids);
}
// Remove duplicates from combined_images
$unique_images = array_unique($combined_images);
// Update the category's ACF gallery with combined and unique image IDs
update_field('mill_category_gallery', $unique_images, 'mill_location_' . $category->term_id);
}
}
}
// Hook into save_post event to replicate images
add_action('acf/save_post', 'replicate_mill_images_to_categories', 20);
Thanks for your help,
Christopher