Attach the uploaded media file to the post

Support MB Frontend Submission Attach the uploaded media file to the post

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #39222
    jak93jak93
    Participant

    Hi, 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

    #39236
    PeterPeter
    Moderator

    Hello 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-parent

    #39812
    AnLipAnLip
    Participant

    I 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 );
    ?>
    #39829
    PeterPeter
    Moderator

    Hello,

    This feature is added to the new version of Meta Box, can you please check if it works on your site?

    #39831
    AnLipAnLip
    Participant

    Hi,

    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.

Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.