1

I am busy with a loop to return all media in the medialibrary in objects. I figured out how to get the images ( with wp_get_attachment_image('$mediaID') where the $mediaID is the id of the image )

But I want to loop all media, not just the images, is there a function that I can use for video's and audio that is also from the medialibrary. I tried get_attached_media but this will only work if the media is actually attached and I want to loop all media.

Is there a way to do this and to get it as objects?

Thanks in advance.

P.S.

The code I am using is:

            <?php
            $query_images_args = array(
                'post_type' => 'attachment', 'post_mime_type' =>'', 'post_status' => 'inherit', 'posts_per_page' => -1,
            );

            $query_images = new WP_Query( $query_images_args );

            foreach ( $query_images->posts as $media) {
                //save the url in a variable to test with
                $media_url = wp_get_attachment_url( $media->ID );

                $mediaID = $media->ID;
                echo wp_get_attachment_image( $mediaID );
            }
           ?>

P.P.S. I do this because I want to show the media in some sort of library on the front end. Later there will be categories like 'video', 'audio' and 'img'

1
  • in your code, is $media not already an object? Commented Jul 30, 2014 at 12:54

1 Answer 1

2

Have you tried to use get_children() function?

Edit:

All media attached to any post is treated as children of it. The function wp_get_attachment_image() will only return images.

With the function get_children() you'll be able to get all the media, images or anything else.

In the above link, you can find some examples.

3
  • Arnau, please be a bit more verbose in your answers. Else the community bot will constantly flag your answers as "low quality". For starters, you might want to explain the why for your answer :)
    – kaiser
    Commented Jul 30, 2014 at 15:46
  • Sorry. I would like to add a simply comment, but i don't have enough reputation. I edited my answer. Commented Jul 30, 2014 at 16:34
  • If you got an answer, don't add a comment. Anwers bring upvotes (like now), bring reputation and capabilities. :)
    – kaiser
    Commented Jul 30, 2014 at 16:37

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.