WordPress.org

Support

Support » How-To and Troubleshooting » Using WP_Query to get attachments

Using WP_Query to get attachments

  • Is there a way to get attachments with WP_Query?

    It seems to work only with get_posts():

    $attachments = new WP_Query('post_type=attachment'); // not working
    
    $attachments = query_posts('post_type=attachment'); // not working
    
    $attachments = get_posts('post_type=attachment'); // works
    
    var_dump($attachments);
Viewing 1 replies (of 1 total)
  • What about using get_children?

    For example, display thumbnail image for post in loop:

    <?php
    $images = get_children(
    array(
    'post_parent' => $post->ID,
    'post_status' => 'inherit',
    'post_type' => 'attachment',
    'post_mime_type' => 'image',
    'order' => 'ASC',
    'orderby' => 'menu_order'
    )
    );
    if ( $images ) {
    foreach ( $images as $id => $image ) {
    $img = wp_get_attachment_thumb_url( $image->ID );
    $link = get_permalink( $post->ID );
    print "\n\n" . '<img class="thumb" src="' . $img . '" alt="" />';
    }
    }
    ?>

Viewing 1 replies (of 1 total)
  • The topic ‘Using WP_Query to get attachments’ is closed to new replies.
Skip to toolbar