How to limit Query Loop on Media Gallery to current post's medias (custom field)?

I’m going to share this for the community and hoping this gets better along the way. I was trying to query the medias uploaded to a post through a custom field (Metabox/Advanced Images) and display them in a Gallery using the Bricks query loop for Medias (Query Loop – Bricks Academy). But since the images are not automatically attached to the current post they are being uploaded to (it is acknowledged by Metabox support), the query using “child of : {post_id}” does not work.

I have found out there are two steps necessary :

#1 Amend Query Post_in For Gallery (given by Bricks support), replace “mg_projet_galerie_images” by your own custom field and “yeamho” by the ID of your element with the query loop. Using this technique, you do NOT set “child of: {post_id}”, you leave it blank :

<?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 );

#2 Make sure the uploaded media attachments are automatically attached to the current post they uploaded to (replace “mg_projet_galerie_images” by your own custom field). It appears for me I do not necessarily need to set the query loop (media type) to “child of: {post_id}” as explained in the Bricks academy tutorial. To automatically attach/detach images to the post use this code (edit : replace with newer one below) :

<?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));
        }
    }
}

Now to make this better, I have to find a way to remove (un-attach) the medias if removed from the current post. I hope this will help some of you.

EDIT 2023/01/13 : I found the way to automatically attach & detach images from post when removed from the custom field (MetaBox Advanced Image) :

<?php
add_action('save_post', 'update_detach_image_data');

function update_detach_image_data($post_id) {
    $current_images = get_post_meta($post_id, 'mg_projet_galerie_images', false);
    if(!empty($current_images)){
        foreach($current_images as $current_image){
            wp_update_post(array('ID' => $current_image, 'post_parent' => $post_id));
        }
    }

    $query_args = array(
        'post_type' => 'attachment',
        'post_parent' => $post_id,
        'post_status' => 'any',
        'posts_per_page' => -1,
        'fields' => 'ids',
        'no_found_rows' => true
    );
    $attachments_query = new WP_Query($query_args);
    $attachments = $attachments_query->posts;

    $detach_images = array_diff($attachments, $current_images);
    if (!empty($detach_images)) {
        foreach ($detach_images as $detach_image) {
            wp_update_post(array('ID' => $detach_image, 'post_parent' => 0));
        }
    }
}
1 Like