• Resolved traveler

    (@visualeight)


    I have a need to upload images to specific client pages (cpt), so for example a client would own all of their images that I do not want associated with another client.

    I would like to upload the images from the client’s post, and have those images automatically attached to a custom taxonomy. What would be the best approach to doing this?

    Currently I’m using ACF for the gallery field on the client page to upload the images. It works, but not ideal. If I upload a group of photos it will not allow me to attach the custom tax to all of the images, so they still have to be clicked and edited in the gallery field of the admin screen of that client’s page.

    Background:
    I plan to ultimately have a function that runs when a new client post is made that also creates the custom taxonomy in that same client’s name. So ‘John Smith’ has his client page titled ‘John Smith’ and the custom tax used to attach images to this client would also be ‘John Smith’. I’d like to take the guesswork out of attaching the correct tax to the images, so that a client page is created, as well as the custom tax, then images uploaded through that client page’s admin/edit screen will automatically be tagged with that client’s tax.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    If images are inserted through the page editor’s UI, they are automatically associated with that page. The related attachment post’s “parent” property will contain the page ID into which the image was inserted. If the user creating the page is also the client you are trying to relate this to, an existing relationship exists by default.

    Never the less, you can automatically assign taxonomy terms to attachment posts if you want. Image uploads have an associated attachment post created through wp_insert_post(), so you could hook one of the actions that fire in the process to assign terms. “save_post_attachment” action ought to work nicely.

    Thread Starter traveler

    (@visualeight)

    Thanks bcworkz.

    The relationship you describe above, when a relationship is established between the page being edited and the images uploaded with that page open, wouldn’t that parent property with the page ID of where they were uploaded be changed if that same image were used in a different page?

    Would this behavior be different between using the gutenberg image field to upload and image vs using the ACF image gallery upload field?

    Any way to make the relationship permanent between an uploaded image and the parent property page ID, so that it can’t be overwritten?

    Do you know where I can find more information on the different attachment properties that mentioned above, and how would you query them by this attachment?

    Moderator bcworkz

    (@bcworkz)

    The attachment’s parent remains the one to which it was originally uploaded. Subsequent reuse does not alter this. The only relationship of reused images to their second and beyond posts is its URL in post content, there’s no formal DB relationship like there is for the first parent. Keep in mind that images uploaded from the media library external to post/page editors have no parent. The attachment parent property is set to 0.

    Something could conceivably overwrite the parent value anyway, it’s not a protected field, but that would be unusual behavior. Normally, once established, the parent remains unchanged.

    I cannot speak for ACF behavior. I’d expect it to be the same, but IDK. You can try asking in the dedicated ACF forum, or check the DB records for yourself through the phpMyAdmin app.

    Attachments are a type of post just like WP pages and posts. They are all defined by the WP_Post class. Attachments do have some meta data unique to that type. Use phpMyAdmin to search postmeta table for a particular attachment ID to see typical meta data saved.

    As post objects, you query for attachments just as you would posts or pages, with WP_Query class. Just include 'post_type'=>'attachment', as one of the arguments. To get attachments of a particular page include 'parent'=> get_the_ID(), as an argument, assuming the query is instantiated from within the loop which is outputting the parent page content. Otherwise get the parent ID by whatever means are expedient.

    Thread Starter traveler

    (@visualeight)

    Thanks bcworkz, this was great info!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom image uploader for cpt and force taxonomy attachment’ is closed to new replies.