WordPress.org

Ready to get started?Download WordPress

Forums

How to get_post_meta() inside foreach loop? (1 post)

  1. marcetin
    Member
    Posted 2 years ago #

    Hello,
    is it possible to do get_post_meta() for every attachment inside foreach loop?
    I am using standard loop for list all attachments but I need to display custom field value:

    <?php
    $args = array( 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => null );
    $attachments = get_posts( $args );
    if ($attachments) {
    foreach ( $attachments as $post ) {
    setup_postdata($post);
    the_title();
    the_attachment_link($post->ID, false);
    the_excerpt();
    }
    }
    ?>

    When I put ->get_post_meta($post->ID, 'MY-KEY', false);<- inside loop there is no output, when it is outside loop I get 'MY-KEY' value from just one, last post.
    I have try with get_the_ID() instead of $post->ID, but no result.

Topic Closed

This topic has been closed to new replies.

About this Topic