Support

Account

Home Forums Front-end Issues get_posts and get custom value from their attachment

Helping

get_posts and get custom value from their attachment

  • Hello

    I’ve got a post_type with value:
    title, desc, kind, image

    I can get all custom value to array from this post_type, but i can’t get information about “image”, only i can get the numerical value from image…

    I want get all custom value with image value to one loop. This is the code:

    
    $args = array(
    	'posts_per_page'   => 5,
    	'offset'           => 0,
    	'category'         => '',
    	'orderby'          => 'post_date',
    	'order'            => 'DESC',
    	'include'          => '',
    	'exclude'          => '',
    	//'meta_key'         => '',
    	//'meta_value'       => '',
    	'post_type'        => 'wiadomosc',
    	'post_mime_type'   => '',
    	'post_parent'      => '',
    	'post_status'      => 'publish',
    	'suppress_filters' => true,
    	'meta_query'	   => array(
    		'relation' => 'OR',
    			array(
    				'key' => 'pokaz_na_glownej',
    				'value' => '1',
    				'compare' => '='
    			),
    			array(
    				'key' => 'pokaz_na_glownej_slideshow',
    				'value' => '1',
    				'compare' => '='
    			)
    	)
    
     ); 
    
    $posts_array = get_posts( $args );
    
    foreach ( $posts_array as $post ) : setup_postdata( $post );
        // $all_key_value have a custom field value, except image information
        print_r ($all_key_value = get_post_meta( get_the_ID(), '', false ));
    endforeach; 
    wp_reset_postdata();
    

    Could you help me?

  • You can’t get the values for an image attached to a post because the meta values for an image are attached to the image. You must to a second get_post_meta supplying the attachment ID as the post id. The attachment ID is what is seen for the image value.
    $image_meta = get_post_meta($attachment_id);
    Hope that helps someone

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘get_posts and get custom value from their attachment’ is closed to new replies.