Attach the uploaded media file to the post
Support › MB Frontend Submission › Attach the uploaded media file to the post
- This topic has 4 replies, 3 voices, and was last updated 2 years, 4 months ago by
AnLip.
-
AuthorPosts
-
November 20, 2022 at 2:17 AM #39222
jak93
ParticipantHi, I have a frontend submit that creates a post with an image upload field, is it possible to attach images to the created post so they appear in the modal media when we filter by "uploaded into this..." thanks
November 20, 2022 at 9:07 PM #39236Peter
ModeratorHello there,
It looks like the field
image_upload
does not set the post parent (Uploaded to) for the uploaded images. I will inform the development team to consider supporting this feature in future updates.Meantime, you can try to set the post parent (Uploaded to) for the images manually. Or if you are familiar with coding, each image is a post (post type attachment), you can update the image data with the WordPress function wp_update_post().
Refer to this topic https://wordpress.stackexchange.com/questions/79934/how-to-update-post-parentDecember 15, 2022 at 4:00 PM #39812AnLip
ParticipantI have found a code to automatically update media attachment to be attached to the current post it was uploaded to :
<?php add_action('save_post', 'update_image_data'); function update_image_data($post_id){ $images = get_post_meta($post_id, 'mg_projet_galerie_images', true); if(!empty($images)){ foreach($images as $image){ wp_update_post(array('ID' => $image, 'post_parent' => $post_id)); } } } ?>
It would be nice to have this feature natively. For example, in Bricks Builder you could query the medias attached to the current post this way : https://academy.bricksbuilder.io/article/query-loop/#media-query
At the moment, if medias are not attached to the post, then you can not query them without adding more custom code such as this one (it was given to me by Bricks support - replace with your own ID and custom field names) :<?php add_filter( 'bricks/posts/query_vars', function( $query_vars, $settings, $element_id ) { if ( $element_id !== 'yeamho') return $query_vars; $gallery_images = (array) rwmb_meta( 'mg_projet_galerie_images', ['size' => 'full'] ); $gallery_images_ids = array_keys($gallery_images); // if no gallery images, set empty string array $gallery_images_ids = count($gallery_images_ids) > 0 ? $gallery_images_ids : ['']; $query_vars['post__in'] = $gallery_images_ids; return $query_vars; }, 10, 3 ); ?>
December 15, 2022 at 11:01 PM #39829Peter
ModeratorHello,
This feature is added to the new version of Meta Box, can you please check if it works on your site?
December 15, 2022 at 11:05 PM #39831AnLip
ParticipantHi,
Well, as of yesterday, it did not work. The Media Attachment is not attached to the current post it is uploaded to. At least, it does not show as attached to when displaying Media Library (in WP backend) as a list, where it should.
using the snippet I provided above, it does work nicely. But if the Media Attachment is removed from the custom field (Advanced Images), it does unfortunately not update the attachment to the post (by removing it).
There is still work to be done, hope MB can implement this all the way.
-
AuthorPosts
- You must be logged in to reply to this topic.