Support

Account

Home Forums General Issues Gallery values disappearing, trying to use get_post_meta

Solved

Gallery values disappearing, trying to use get_post_meta

  • I’m outputting a post with gallery and image fields, but am running into an issue where in some cases, all the image fields are returning a value of “1” instead of the actual URL, title, caption, etc values. The issue happens when I apply a date filter to the get_posts argument for a set of content, and only when the start date of the filter is more recent than the oldest of the images in my media library. For example, the oldest library image in is Dec. 1, 2014. If the start date of the filter is Dec. 2 or later, the values are all output as “1”. If the start date of the filter is Dec. 1 or earlier, all the values are displayed correctly.

    A workaround I found for a custom image field seemed to work:

    $thumbValues = get_post_meta( $post->ID, 'thumbnail');
    $thumbImage = wp_get_attachment_image_src( $thumbValues[0], 'medium' ); 
    echo $thumbImage[0]; 

    However, I’m having trouble getting this to work for the gallery field. How can I use get_post_meta to output gallery items?

    Or is there a way to fix it so that I can use the standard ACF of outputting data?

  • I think I figured this out. Here’s the code I used to get the URL, credit and description field from the gallery field “photos”:

    
    $photosValues = get_post_meta( $post->ID, 'photos');
    	foreach ( $photosValues as $photoItem) { 
    		foreach ( $photoItem as $photoID) { 
    			$thePhoto = get_post($photoID);
    			$photoURL = wp_get_attachment_url($photoID); // gets photo URL
    			$photoCredit = get_field('credit', $photoID); // gets ACF credit field
    			$photoDesc = $thePhoto->post_content; // gets description field
    			echo $photoURL; 
    			echo $photoCredit; 
    			echo $photoDesc;	
    		} 
    	}
    
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Gallery values disappearing, trying to use get_post_meta’ is closed to new replies.