I am trying to get the IDs of the files uploaded

Hi all,

I am trying to get the IDs of the files uploaded to the WordPress Media Library via a Bricks form, to assign them as a featured image and gallery of a custom post (restaurants), but I can’t retrieve them in any way. I have tried several approaches and would like to share all the attempts made so far.

Context

  • Objective: Get the ID of the featured image and gallery images uploaded via the Bricks form and associate them with the corresponding post.
  • Configuration:
  • The main image should be saved as a “featured image” of the post.
  • The gallery should be saved in the meta field restaurant_gallery as a comma-separated string of IDs.
  • The files are actually uploaded to the Media Library.

Attempted Approaches

1. Using $_FILES

  • I checked if the files were uploaded with $_FILES, but Bricks does not use this method for files already assigned as “attachment”.

2. Using get_uploaded_files()

  • I tried to retrieve the files with $form->get_uploaded_files() and the files are listed with location => attachment.
  • However, the returned array only contains the file path and URL, but no attachment ID.

3. Using attachment_url_to_postid()

  • I tried to convert the URLs provided by Bricks to attachment IDs with:
$attachment_id = attachment_url_to_postid($image_url);
  • But the returned ID is always 0, suggesting that the attachment is not yet registered in the database at the time of the call.

4. Manual database query

  • I tried to look up the ID directly in the database:
global $wpdb;
$attachment_id = $wpdb->get_var($wpdb->prepare(
    "SELECT ID FROM {$wpdb->posts} WHERE guid = %s OR post_name = %s",
    $image_url, basename($file_path, '.jpg')
));
  • Again the ID is not found.

5. Delay ID lookup

  • I assumed that Bricks doesn’t immediately register the file as an attachment, so I tried to do the ID lookup after a small delay with wp_schedule_single_event(), but to no avail.

Debug Log

Here is an example of the data returned by the Bricks form:

[uploaded_files] => Array (
  [form-field-jwymyi] => Array (
    [0] => Array (
      [file] => /home/site/wp-content/uploads/2025/02/image.jpg
      [url] => https://site.com/wp-content/uploads/2025/02/image.jpg
      [type] => image/jpeg
      [name] => image.jpg
      [location] => attachment
    )
  )
)

There is no trace of the attachment ID.


Questions

  • How ​​can I get the ID of the attachments uploaded through the Bricks form?
  • Is there a Bricks internal method to retrieve the IDs directly from the form?
  • Is there a way to force Bricks to return the attachment ID?

Any suggestions are welcome! Thanks in advance for your help.

Of course this is a request that goes beyond my specific need to populate two fields. It remains a fundamental question because if I want to create a post using the Bricks form there is no direct way to set an uploaded file that could be the preview image as such.