• Hey Edward,

    I have a number of custom post formats that contain custom fields pointing to attachments. So for instance I have a “Photos” post format that contains three thumbnails, which are attached by custom fields (creatively named “image1”, “image2”, and “image3”).

    While the Broadcast plugin correctly finds these attachments and moves them over to the correct blog, the post IDs are not updated in the custom fields. This creates some more work for my users who then have to go into the gallery, figure out which images were originally attached, and re-attach them.

    Rather than deal with all of that junk, I fixed the plugin. Essentially it will search the post’s custom fields for IDs that were pointing to attachments, and convert them. I’m not sure of the best way to submit this, so I’m just going to attach some code:

    At line 1541: (just past wp_delete_attachment( $attachment_to_remove->ID );

    $converted_attachment_IDs = array();
    foreach( $attachment_data as $key=>$attached_file)
    {
    	if ( $key != 'thumbnail' )
    		$converted_attachment_IDs[$key] = $this->copy_attachment( $attached_file, $new_post_id );
    }

    At line 1567: (just past $single_meta_value = maybe_unserialize( $single_meta_value );)

    // If this was a custom field pointing to an attachment, convert the ID
    if(array_key_exists($single_meta_value, $converted_attachment_IDs)){
    	$single_meta_value = $converted_attachment_IDs[$single_meta_value];
    }

    At line 1574: (just past $meta_value = maybe_unserialize( $meta_value ); )

    // If this was a custom field pointing to an attachment, convert the ID
    if(array_key_exists($meta_value, $converted_attachment_IDs)){
    	$meta_value = $converted_attachment_IDs[$meta_value];
    }

    Let me know if this makes sense – I have subbed to email notifications on this thread so I will get the messages & can send the full file / a patch if needed.

    https://www.ads-software.com/extend/plugins/threewp-broadcast/

Viewing 5 replies - 1 through 5 (of 5 total)
  • For the Googlers that enter this page searching for the same problem;

    I was facing issues with the Advanced Custom Fields (ACF) plug-in. It copied ACF images, but it did not copy the attachment. I added this to OP’s code;

    After $bcd->post_custom_fields = get_post_custom( $bcd->post->ID ); add:

    $acf_image_IDs = array();
    foreach ($bcd->post_custom_fields as $k => $pcf)
    {
    	if (substr($k, -6) === '_image' && $k{0} !== '_')
    	{
    		$a_id = $pcf[0];
    
    		$fake_attachment_object = new \stdClass;
    		$fake_attachment_object->attachment_id   = $a_id;
    		$fake_attachment_object->attachment_data = attachment_data::from_attachment_id( $a_id, $bcd->upload_dir);
    
    		$acf_image_IDs[$a_id] = $fake_attachment_object;
    	}
    }

    Below $converted_attachment_IDs = array(); add:

    foreach ($acf_image_IDs as $acf_image_ID => $data)
    {
    	$converted_attachment_IDs[$acf_image_ID] = $this->copy_attachment( $data, $new_post_id );
    }

    Since ACF also requires some post meta fields prefixes with an underscore, you need to whitelist the fields you wish to copy. For example, I had a field called carrousel-for-content which has the internal name of _carrousel-for-content. Once I whitelisted the key, everything worked smoothly. This setup does assume all ACF settings are the same for every site in the MU environment by XML import.

    Plugin Author edward_plainview

    (@edward_plainview)

    This sounds like it should be converted to a separate ACF-compatability plugin…

    I agree. ??

    Plugin Author edward_plainview

    (@edward_plainview)

    If I’m to write an ACF-compatability plugin then somebody is going to have to get in touch with me and help with configuration and testing.

    [email protected]

    Plugin Author edward_plainview

    (@edward_plainview)

    There is now an ACF-compatability plugin in the premium pack.

    See: https://plainview.se/wordpress/threewp-broadcast-premium-pack/#acf

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Patch: Convert Custom Fields pointing to attachments’ is closed to new replies.