I am trying to create a function that automatically adds a value to the attachment metadata (if it matters, on Audio uploads).

For example, I want to add a value of 'artist' and output it as my own specificity (for the sake of the example, just simply 'test')

I've tried numerous things but each one, thus far, has not worked (and outputs an unspecified error upon uploading the media).

Here's a couple things I've tried:

function auto_update_audio_meta($post_ID) {
add_post_meta( $post_ID, 'artist', 'test');
add_action('add_post_meta', 'auto_update_audio_meta');

I've also tried hooking to update_post_metadata, and variations such as

function auto_update_audio_meta() {
wp_update_post_meta( $post->ID, 'artist', 'test');
add_action('update_post_metadata', 'auto_update_audio_meta', 10, 5);

What am I doing wrong?


1 Answer 1


You're close! Try using these hooks instead.

// Add post meta to new audio uploads.
function auto_update_audio_meta( $post_ID ) {
  if ( wp_attachment_is( 'audio', $post_ID ) ) {
    add_post_meta( $post_ID, 'artist', 'test' );
add_action( 'add_attachment', 'auto_update_audio_meta' );

For attachment updates

// Update post meta to updated audio uploads.
function auto_update_audio_meta( $post_ID, $post_after, $post_before ) {
  if ( wp_attachment_is( 'audio', $post_ID ) ) {
    update_post_meta( $post_ID, 'artist', 'test' );
add_action( 'attachment_updated', 'auto_update_audio_meta', 10, 3 );
  • Thank you for the reply and edits. I tried your first code, checked the backend details, and also used get_post_meta on the attachment to see an array of it's values. artist was not listed. Hooking to add_attachment was the first thing I tried, and had since moved past it since I did not think it was the appropriate hook because it fires before metadata is generated. See the first answer at : wordpress.stackexchange.com/questions/61519/…
    – bbruman
    Commented Sep 16, 2016 at 6:09
  • The reason why I chose update_post_metadata was after reading the top answer in this thread : wordpress.stackexchange.com/questions/16835/… - but I could not get it to work. Also it is important to note that I am not looking to GET the attachment metadata, only add a value to it, so I'm not sure exactly where this could be hooked onto..
    – bbruman
    Commented Sep 16, 2016 at 6:12
  • Err--- actually nevermind, it worked, thanks! It just wasn't appearing where I was expecting it. I was looking for it to be in _wp_attachment_metadata with all the other metadata? Instead it's in the _wp_attached_file array which includes the url and my new added value. Anyways, I will kindly accept your answer :)
    – bbruman
    Commented Sep 16, 2016 at 6:16
  • Glad it worked out. I made a few edits so you might've used the broken code first.
    – cowgill
    Commented Sep 16, 2016 at 6:19

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.