0

Like it says on the tin - Im trying to create a list of post attachments that includes everything except say audio, which I'll feed into a player instead.

  • The filter needs to be generic: 'audio' not 'audio/mpeg'.

(For some reason if ( $mime !== 'audio/mpeg' ) will filter out valid mp3 files –– weird.)

So the following dosen't work:

$children = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment' ) );

            if ( !empty( $children )){
                foreach ( $children as $attachment_id => $attachment ) {
                    $mime = get_post_mime_type($attachment);
                    $url = wp_get_attachment_url( $attachment->ID, false );  
                        if ( $mime !== 'audio/mpeg' ) {                                                             
                                $content_attach .= '<a href="' . $url . '"  download="' . $url . '"> ' . $title . ' | ' . $mime . '</a>';
                        }
                }       
             }  
echo apply_filters('the_content', $content_attach ); 

Further listing every audio mime type like so: if($mime !== 'audio/mpeg' || $mime !== 'audio/ogg') etc will still leave out those crazy mp3 files.

What does work is:

$children = get_children( array('post_parent' => $post->ID, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'audio' ) );

which will return all the attached audio types in one go - great my player can handel it.

Now I need to omit everything that isn't audio and list it.

Does anyone have any ideas?

and thanks again for your help and experience. . .

  • if($mime !== 'audio/mpeg' || $mime !== 'audio/mpeg') is the same as »If: it is NOT AudioMpeg OR it is NOT AudioMpeg« ... doesn't make much sense asking the same twice, eh? – kaiser Apr 11 '12 at 22:27
  • yup! that's a typo - should read if($mime !== 'audio/mpeg' || $mime !== 'audio/ogg') –– I've corrected it. – orionrush Apr 11 '12 at 22:39
2

Looks like this is two questions: how to get all the children that are audio, and how to get all the children that aren't audio.

1) The post_mime_type parameter of get_children can actually take a wildcard, so you could use:

$children = get_children(array(
     'post_parent' => $post->ID,
     'post_status' => 'inherit',
     'post_type' => 'attachment',
     'post_mime_type => 'audio/%'
));

2) You can't do a mime-type exclusion using get_children, so you're better off just filtering the whole list to exclude them that way. I think you've got the right idea, just use something like this to exclude all the different kinds of audio:

if ( ! strstr($mime, 'audio/') ) {  // ...etc
1

I know this is an old post but it may help someone

    $mime_types = get_allowed_mime_types();
$remove_mime_types = array(
// Image formats
'jpg|jpeg|jpe'                 => 'image/jpeg',
'gif'                          => 'image/gif',
'png'                          => 'image/png',
'bmp'                          => 'image/bmp',
'tif|tiff'                     => 'image/tiff',
'ico'                          => 'image/x-icon'
);

$mime_types = array_diff($mime_types, $remove_mime_types);

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.