Support

Account

Home Forums General Issues Gallery: Related Images "Uploaded to"… Problem!

Solving

Gallery: Related Images "Uploaded to"… Problem!

  • I’m using WordPress as a straightforward gallery, with all the images accessible via the homepage. Each attachment page features the relevant image, other content and ‘related images’, which link to other images in the gallery. I’m using the ACF Repeater (Image) field (not ACF Gallery field) to ‘attach’ these images in the WordPress back end.

    Related Images: Front and Back End

    It seems to work perfectly, except that every time I save changes made to any image (or rather its fields), the permalink structure of the related images are changed. Whereas before they’d be http://gallery/image-title-1, they then become children of the image they are attached to, i.e. http://gallery/parent-image/image-title-1/. This is due to the WordPress “uploaded to” feature, in cases where you’d want the image ‘post’ to be a child of a parent post.

    It is possible to manually detach them in turn to fix the problem, but I would like to remove this need entirely, since other users will be populating the gallery. I hoped I’d find an easy solution, but I’m not really as advanced with filters etc to know where to start doing it manually. Tried a few plugins that claimed to suppress hierarchies or manipulate attachments but no joy. My permalinks are simply set to “post name”:

    Permalinks

    Is there any way of using the ACF Image field without incurring this behaviour, and if not, how can I automatically remove the hierarchical effect its having on the permalinks?

    Any help hugely appreciated.

  • I’m running into the same problem; did you ever find a fix for this?

  • I don’t think I ever did find a solution I’m afraid; it’s been a while now since I’ve looked at it, but I think I resolved to reset the permalink manually each time. It works, but it’s hardly a decent fix.

  • I asked support for assistance and they suggested: “You can make use of the acf/save_post filter to detach any media from the post.”

    I’m not a programmer so I have no idea where to start with that, but I thought I’d share it in case it’s useful to you.

    I’ll let you know if I ever figure out a solution.

  • 
    add_action('acf/save_post', 'remove_att_from_att');
    function remove_att_from_att($post_id) {
      $post_type = get_post_type($post_id);
      if ($post_type != 'attachment') {
        return;
      }
      $children = get_children($post_id);
      if (children) {
        // remove filter so this filter does not run again
        remove_filer('acf/save_post', 'remove_att_from_att');
        foreach ($children as $child) {
          $child->parent = 0;
          wp_update_post($child);
        }
        // restore filter
        add_action('acf/save_post', 'remove_att_from_att');
      }
    }
    
  • Oh, I completely missed this! I’m so sorry. Thanks so much for your help. The formatting got a bit wonky but I fixed it up as best as I could and ended up with this code:

    
    add_action('acf/save_post', 'remove_att_from_att');
    function remove_att_from_att($post_id) {
    	$post_type = get_post_type($post_id);
    		if ($post_type != 'attachment') {
    			return;
    		}
    	$children = get_children($post_id);
    		if ($children) {
    			// remove filter so this filter does not run again
    			remove_filter('acf/save_post', 'remove_att_from_att');
    			foreach ($children as $child) {
    				$child->parent = 0;
    				wp_update_post($child);
    			}
    		// restore filter
    		add_action('acf/save_post', 'remove_att_from_att');
    		}
    }
    

    Which, unfortunately, didn’t work; the images still get attached every time I save a post. Did I make a mistake with my tweaks or is there something else missing?

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Gallery: Related Images "Uploaded to"… Problem!’ is closed to new replies.