So i want to create a gallery of all the pictures from all the posts into one page.

I tried different plugins but they need constant maintain i.e. I have to go add the new picture in the gallery plugin everytime.

What I want this gallery to be able to do is to auto-generate or auto-pull all the pictures from all the posts. So my client or I do not need to update both the post as well as the gallery everytime.

However, I have no idea how to do this! haha

Your help and kindness would be greatly appreciated!

Thank you in advance!


1 Answer 1


Wordpress saves all images as a post type, called attachments. You will need to run a query to retrieve these attachments. You also need to specify which type of attachments you want to retrieve, as all attachments aren't images. Videos, pdf files and audiofiles are also attachments, so you will need to specifiy the mime type to be images. Here is the query to retrieve all images

$new_query = new WP_Query(array(
    'posts_per_page'   => 1

while ($new_query->have_posts()) : $new_query->the_post();
    $args = array (
        'post_type' => 'attachment',
        'numberposts' => -1,
        'status' => 'publish',
        'post_mime_type' => 'image',
        'parent' => $post->ID

    $attachments = get_posts($args);

    if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
            echo '<div class="oneCell">';
            echo '<a href="' . get_permalink( $post->ID ) . '">';
            echo wp_get_attachment_image( $attachment->ID, 'full' );
            echo '</a>';
            echo '</div>';

To further your reading and to learn more about the uses of WP_Query, check out the codex

  • Did my answer solve your problem. If so, please accept my answer by clicking on the check mark below my answer. If not, what are you still having a problem with. Let me know so that I can assist you further in solving your problem Jun 1, 2014 at 6:38
  • Yep it works perfectly. someone told me i should use the 'numberpost => -1' instead of a '56' so it's infinite? i could be wrong.
    – klaoha06
    Jun 1, 2014 at 15:58
  • By the way, I would really love to learn to manipulate WP. is the Codex the place I should start? If there is any other recommendation I am all open :) thank you very much!
    – klaoha06
    Jun 1, 2014 at 16:01
  • O yes, I copied the code from my theme, totally forgot to change 56 to -1. -1 means no limit, all posts will be fetched. The codex is an excellent place to start. You should also make use of the default themes, have a look at how they are coded. That is the correct standard. Jun 1, 2014 at 16:09

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.