Add a term to an attachment submitted from front end

I have a front end post form where users can upload a file with their post that I save as an attachment using media_handle_sideload(). I need a way to add a term to that attachment so I can show a loop of all specific attachments by a user with that term.

Related posts

Leave a Reply

1 comment

  1. media handle sideload returns an attachment ID, and attachments are normal posts with the post type ‘attachment’. All the same things apply, featured images parents taxonomies post meta etc albeit with a few attachment oriented functions like wp_get_attachment_image

    So use wp_set_object_terms as you normally would, e.g.:

    $id = media_handle_sideload(.... 
    if(!is_wp_error($id)){
        wp_set_object_terms( $id, array(terms...), $taxonomy, $append );
    }