0

I am trying to fetch all audio metadata when I upload a file.

This all happens in a plugin, and I currently use the add_attachment action to trigger my custom function :

// link
add_action( 'add_attachment', 'add_metadata' );

// Definition
public function get_metadata( $post_ID ) {
    $post = get_post( $post_ID );
    var_dump( $post );
    var_dump( wp_get_attachment_metadata( $post_ID ) ); 
}

The $post contains a lot of information, object(WP_Post)#375 (24) { ... }, but the wp_get_attachment_metadata( $post_ID ) is "".

I suspect that all the metadata is not generated at the time of the upload, but still I am able to fetch them inside The Loop on a custom page with the following code :

while ( have_posts() ) : the_post(); 
    $args = array(
        'post_type'   => 'attachment',
        'numberposts' => -1,
        'post_status' => null,
        'post_parent' => $post->ID,
    );    
    $attachments = get_posts( $args );
    if ( $attachments ) {
        foreach ( $attachments as $post ) { 
            $attachment_metadatas = wp_get_attachment_metadata( $post->ID );
            echo '<pre>' . var_export( $attachment_metadatas, true ) . '</pre>';
        }
    }
endwhile;

Do you have an idea of what I can try?

1

Grab the data with a filter:

add_filter('wp_update_attachment_metadata', 'grab_meta_data');
function grab_meta_data($data, $post_id) {
    // inspect $data here
    return $data;
}
  • This is great, thanks a lot :) I may need to understand more the difference between a filter and an action. – nobe4 Nov 1 '15 at 16:25
  • @Nobe4 : actions and filters – s_ha_dum Nov 1 '15 at 18:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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