Cannot get the attachments/media from a custom WordPress post type?

I’m attempting to get the attached/inserted images in my WordPress php code that I inserted into a test post in the admin interface. I clicked the “Add Media” button and uploaded my pictures and updated the post (where front-page is the custom post type).

(I clicked this button):

Read More

enter image description here

However, I can’t seem to retrieve the picture associated with that post for the life of me. If I set a Feature Image (thumbnail), I can get that, but not inserted images. Here’s what I’ve tried:

Looping Through Attachments (No luck):

$attachments = query_posts(
                array(
                'post_type' => 'front-page',  // only get "attachment" type posts
                'post_parent' => $post->ID,   // only get attachments for current post/page
                'posts_per_page' => -1        // get all attachments
              )
            );
foreach($attachments as $attachment) {
    echo get_the_ID();
    echo wp_get_attachment_image( $attachment->ID, 'full' );
}

wp_attached_image(post ID) (No luck):

wp_get_attachment_image( $post->ID );

Getting all the posts (No luck):

<?php
$args = array( 
    'post_type' => 'front-page', 
    'post_mime_type' => 'image',
    'numberposts' => -1, 
    'post_status' => null, 
    'post_parent' => $post->ID 
); 
$attached_images = get_posts( $args );
echo $attached_images[0];
?>

Getting the post gallery images (No luck)

get_post_gallery_images( $post->ID );

I’m really lost as to why I can’t retrieve the images. I’ve exhausted nearly every suggestion I could find online, and was wondering if it had something to do with my custom post type or what? Any help would be appreciated.

Related posts

4 comments

  1. I think you are missing the resulting code with echo (as I am doing below) or print or output.
    I managed to do it with this code a while ago.. hope it helps-(using fancybox in this one)

    <?php
    $attachments = get_posts( array( 
    'post_type' => 'attachment',
    'post_mime_type'=>'image',
    'posts_per_page' => -1,
    'post_status' => 'any',
    'post_parent' => $post->ID)
    );
    
    if ( $attachments ) {
    foreach ( $attachments as $attachment ) {
    $src = wp_get_attachment_image_src( $attachment->ID, full);     
    $html = '<a class="fancybox" href="'.$src[0].'">';
    $html .= wp_get_attachment_image( $attachment->ID, 'gallery-thumb') .'</a>';
    echo $html;
    }
    }
    ?>
    
  2. This is how I ended up fixing it:
    (Commented so as to explain what I did).

    <?php
            // Argument array to hold parameters for get_post() method.
            $args = array(
                'post_type' => 'attachment', 
                'posts_per_page' => -1, 
                'post_status' => 'any', 
                'post_parent' => null
                );
    
            // Holds the URL of the image.
            $image_url = '';
    
            // If we have a thumbnail in the post, use that as the main image.
            if (has_post_thumbnail($post->ID)) {
                $image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID))[0];
            }
            // Otherwise, if we have an attachment image, use that as the main image.
            else if (get_posts($args)) {
                // Simple test to see if we can display attachments from any post.
                $attachments = get_posts($args);
                if ( $attachments ) {
                    $first_photo = $attachments[0];
                    $image_url = wp_get_attachment_url($first_photo->ID , false );
                }
            }
            // If there are no images, just display the text. 
            else {
                echo "No images";
            }
        ?>
    
  3. <form id="form1" name="form1" method="post" enctype="multipart/form-data" action="">
         <input name="name" type="image" src="resimler/azalt.gif" />
    </form>
    

    try this.

    Print_r($_Request); where you want to see . it will work .

Comments are closed.