Support

Account

Home Forums ACF PRO Dynamically restrict media modal attachments Reply To: Dynamically restrict media modal attachments

  • Hi @verde

    You should be able to do it by using the “pre_get_posts” hook like this:

    add_action('pre_get_posts','my_acf_image_change_query');
    function my_acf_image_change_query( $wp_query_obj ) {
     
        // only do this on certain field
        if( isset($_POST['query']) && $_POST['query']['_acfuploader'] == 'field_1234567890abc' ){
            
            // get the current post ID
            $post_id = $_POST['post_id'];
            
            // get the relationship raw value
            $relationship = get_field('custom_field_name', $post_id, false);
            
            // set the parent query
            $wp_query_obj->set( 'post_parent__in', $relationship );
        }
        
    }

    Where “field_1234567890abc” is the field key.

    I hope this helps 🙂