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 );

Leave a Reply

Your email address will not be published. Required fields are marked *