Looper To Provide Image Based on ACF Field

Hi,

I am trying to create a looper which brings through images based upon ACF Pro fields.

Each image in the Media Library is given an ACF field called Mill Location using the details in the screenshot below.

In Cornerstone I have a simple Grid with a Cell which contains an image element.

The Cell has a looper provider and consumer. The provider is using the WPQuery: post_type=attachment&post_mime_type=image&numberposts=-1&meta_query%5B0%5D%5Bkey%5D=mill_location&meta_query%5B0%5D%5Bvalue%5D=Stroud

The Image source is: {{dc:post:featured_image_id}}.

What I hope to see is a grid of all images from the Media Library which have the ACF field Mill Location set to Stroud.

Is this actually even possible to achieve?

Thanks,
Christopher

Hey Christoper,

Based on your post details, the query string is correct and this should work for you. Did you already set this up on a page? Please share the URL with us so we can check it out. We would also love if you can include WP details. You can create a secure note in your next reply with the following info:
– Link to your site
– WP login URL
– WP username
– WP password
– WP Administrator Role
– Confirmation that we can access and make changes to your site

To know how to create a secure note, please check this out: How The Forum Works

image

Best Regards.

Hi @Ruenel,

Thanks for the reply. Yes, I do have a working example - details in Secure Note.

Thanks,
Christopher

Hey Christopher,

You seem to forget to send the password so we can log in. We would live to check your Image field and your Looper settings to get this issue resolved.

Thank you.

Sorry - My bad!

Hey @whitemedia,

The password is still not working for me.
Please double-check.

Thanks.

It has been reset again. Thanks.

Hey @whitemedia,

I am just wondering if I am checking the correct image here:

This does not have any category selected. Be advised that you are displaying the attachments and not the custom post type Mills item that is using the image here.

Best Regards.

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

1 Like

Hey Christopher,

We’re glad that you’re able to find a solution to your issue.

Thank you.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.