I know the value of a image custom field will give me the attachemnt ID. In a loop over all custom fields, how can I found out if the field is an image? And then get the image URL of the attachemnt?

My code:

$args = array( 'post_type' => array( 'my_type'), 
               'posts_per_page' => 1000, 
               'fields' => 'all' );
$loop = new WP_Query( $args );

// Custom fields being attached
$retArray = array();
while ( $loop->have_posts() ) : $loop->the_post();
    $custom_fields = get_post_custom($post->ID);  

    // Some code to get the actual image url from fields that are images:
    // MISSING!

    // Combine $post and custom field array to new array
    array_push($retArray, array('post' => $post, 'fields' => $custom_fields));

1 Answer 1


If its your own custom post type you should know the field names? Iterate through the fields - or better just get the required field - an then use the attachment Id with wp_get_attachment_image_src.


Chech the codex for further examples and Google for "WordPress custom post type fields".

  • Answers should be more than plain links. They should actually be an answer instead of a route where someone will maybe find an answer. Please help preventing link rot, edit your answer and provide the needed information that helps the OP as well as later visitors to solve their problem.
    – kaiser
    Commented Apr 22, 2014 at 17:29
  • Thanks rockaut, yes I do know the names of the fields. Would have loved to have them "translated" by default though. I think I can live with this though.... Commented Apr 22, 2014 at 17:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.