Support

Account

Home Forums Backend Issues (wp-admin) Check if file id is attached to post

Solving

Check if file id is attached to post

  • Hey there!

    I am currently creating a script that allow users to download private files, but only if the files are attached to the users own post. I am passing the the file id to the function, but before allowing the user to download it, I need to check whether the file is attached to the post in one of several file fields.

    I have tried both looking for the file id in get_post_custom(), and by using get_attached_media, however I didn’t find it in the first function, and the second requires a media type, which I cant know for all files.

    How is the best way to check a file id against all file fields in the post?

    Thanks!

  • If you have the file ID you can either do a query of posts using WP_Query for posts that belong to the user and a meta_query to look for the attachment id in in the field where it is added.

    This is only an example, you’ll need to alter to your values and test

    
    $user_id = get_current_user_id();
    $args = array(
      'post_type' => 'the-post-type',
      'author' => $user_id,
      'posts_per_page' => 1, // only need to see if one exists
      'meta_query' => array(
        array(
          'key' => 'custom_field_name',
          'value' => $file_id
        )
      )
    );
    $query = new WP_Query($args);
    if (count($query->posts)) {
      // this file is attached to at least one post by this user
    }
    
  • Thanks for your suggestion. However, my problem is that I have multiple file fields on my post type. I need to run a query that determines whether the file in question is attached to one of them 🙂

  • Then you need to have multiple fields in the meta query with an OR relationship between the fields. For more information look at https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

  • Thanks. I guess that could work.

    However, each of my users only have one post attached. So what I was hoping to do, was something along the lines of getting an array of all fields from that particular post, and looking up whether the file id in question was present in any of those fields. Would this be possible? Please bear in mind, that the file could be present in a repeater field.

    Thanks for the amazing support so far,

  • You can get an array of all of the fields on a post by using get_fields() https://www.advancedcustomfields.com/resources/get_fields/. I don’t know if this will be easier or not. Honestly, I don’t see anything I’d consider “easy” to accomplish what you’re doing.

    I’d probably look in a different direction depending on how the files get attached to a post belonging to a user. For example maybe adding a field to attachments that records the user that uploaded it if the user is attaching them or if an admin is making the decision then a field where the admin sets who can download them. There are probably other solutions, still, none of them easy.

  • Thanks for your suggestions. I will look into the different possibilities.

    One last question though – is it possible to get all fields of a certain field type? i.e. – all file fields (including those in repeater fields) from the post with id xx?

    Thanks!

  • You might want to look into WP functions and possibly add a field to attachments that tell who uploaded the file. If you want to allow others besides the person that uploaded it then maybe create some type of additional settings for attachments that let you choose who can download it.

    There isn’t any way to get a list of images uses in any field that’s an image field. There’s nothing in the DB that indicates that a field is for an image other than the field definition in ACF. I think that looking for images attached to the user’s posts will be just as convoluted, if not more so, than getting the users posts and looking through them to find images and comparing them to the image they are trying to download.

    You could also alter the path where the images are stored, places each users files in a separate folder, perhaps. I have done this in this file for one of my plugins https://github.com/Hube2/blunt-gated-content/blob/master/admin/acf-upload-path.php

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.