0

Do custom post type attachments store the parent post ID, and if so, how do I retrieve it.

I need to prepend the ID of a CPT post to the uploaded media name, eg 123-my-file-name.pdf

I do have this question posted on Wordpress.org forum and someone is kindly helping me there, but first I need to establish that what I'm doing is actually possible in WORDPRESS. I'm amazed I haven't found any snippet for this.

For those interested, this is my current code in functions.php using WORDPRESS filters which results in nothing being added to the front of the file name.

add_filter( 'wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter($file) {


   // Get the parent post ID, if there is one
       if( isset($_REQUEST['p']) ) {
           $post_id = $_REQUEST['p'];
      } else {
         $post_id = false;
       }
        
    $file['name'] = $post_id . '-' . $file['name'];
    return $file;
}

I'm sorry if I'm breaking protocol by posting here as well. But if I can establish that getting the CPT post id is possible at the point of creating a new post and uploading file I can at least continue trying to get the code to work.

Cheers, Tracy

3
  • I saw this post: wordpress.stackexchange.com/questions/184557/… and the suggested fixed don't work. IE making the CPT hierarchical did not work. And the posted code ended in a fatal error. Sep 22, 2021 at 23:38
  • I tried to answer this using general WP knowledge but only ACF support or ACF developers can answer this. This is hyper specific to ACF forms, and cannot be answered without specialist ACF knowledge and expertise. There is no general WordPress framework or mechanism to help here
    – Tom J Nowell
    Sep 23, 2021 at 1:05
  • Otherwise, ACF and other 3rd party plugin/theme dev support is off topic and not in this stacks scope. You should ask via their official support routes or in their groups and communities.
    – Tom J Nowell
    Sep 23, 2021 at 1:05

1 Answer 1

0

custom post type attachments

There are only attachments, this phrase does not make sense and is not something that is possible. When you upload a file, a post of type attachment is created.

This attachment is attached via post parent. An attachment with no post parent is unattached.

ACF will be creating a post of type paper, whose post ID is provided as the parent. There are 2 posts here. A paper post, and an attachment post that is attached to it. Attachments are just attachments, they work the same everywhere, it does not matter what the type of the post it's attached to is. You get the parent the same way you get any other post, there should be no special step just for CPTs ( CPT's are just post types that don't come with WP by default, the custom part is that your code registered it, not core, otherwise they are stored work and behave the same way ).

For those interested, this is my current code in functions.php which results in nothing being added to the front of the file name.

The problem now is that you're trying to interface with a bespoke 3rd party plugin that may or may not provide this post ID.

In the strictest sense, the only canonical general WordPress answer is that there is no post parent, the attachment post has not been created yet, we do not know.

I'm sorry if I'm breaking protocol by posting here as well. But if I can establish that getting the CPT post id is possible at the point of creating the post and uploading file I can at least continue trying to get the code to work.

To get the answer/solution you want, you will need ACF help, not WordPress help. You need to contact ACF support, ACF developers, and ACF users. ACF knows which post it needs to be attached to, so how does it know that information? That's what you need to ask.

9
  • Hi, I'm a bit disappointed at the reaction here. It was a general question. I could be using any form or even Wordpress's own media gallery. The question would be the same. Do Custom Post Type attachments have the post Id stored? That's ALL I need to know. If I wanted to add the id of the post related to an attachment uploaded by the Wordpress media gallery, how would I do that. Is it possible? But you all got hung up on ACF. Sep 24, 2021 at 18:25
  • It is not a general question, you are mistaken. There is no such thing as a "Custom Post Type attachment", that's not how uploads work, attachments are always posts of type attachment. An attachment is "attached" to its post parent aka your paper post that was created by ACF forms. There is no general/generic WordPress solution to your problem. Until the attachment has been uploaded and created there is no post parent.
    – Tom J Nowell
    Sep 24, 2021 at 22:36
  • Because you're using wp_handle_upload_prefilter the post object has not been created yet, it's too early. To get the ID of the paper post ACF created, you need to know how ACF handles the forms so that you can grab its ID. Otherwise you would need to wait until after the file haas been uploaded and the attachment created, and grab its parent ID. By then it is too late and the filename has been set already.
    – Tom J Nowell
    Sep 24, 2021 at 22:39
  • Okay. Tom, that answers my question. What I'm trying to do is not possible at the creation stage in Wordpress. So the only solution is to create a copy of the folder in /uploads/, ie zip archive, and somehow run through the files and prepend the post-parent id before the file is put in the zip archive. Sep 25, 2021 at 23:55
  • No that would probably break the attachments, and get slower as the folder gets larger. There are better ways of doing it, what you want should be possible, but it requires some knowledge of ACF forms to build it. ACF very likely has hooks you can use or parameters that you can use to grab the ID you need easily, but you need to ask about that in an ACF community.
    – Tom J Nowell
    Sep 26, 2021 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.